我对Rails相对较新,我遇到了似乎常见的问题,但在阅读了所有回复之后,我似乎无法为我的情况做任何工作。
我有一个跟踪贷款和用户的应用,我正在尝试添加付款功能。当我尝试访问付款页面时,我遇到了TypeError: no implicit conversion of Symbol into String
。我尝试过很多东西,但似乎没什么用。我不确定我是如何尝试访问loan_id或其他内容的。
PaymentsController #create
中遇到错误以下是我的观点:
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-2">
<ul>
<li><strong>Lender</strong>: <%= @loan.lender.username %></li>
<li><strong>Borrower</strong>: <%= @loan.borrower.username %></li>
<li><strong>Amount Remaining</strong>: <%= @loan.amount %></li>
<li><%= link_to "New Payment", loan_payments_path(@loan.id), :method => :post %></li>
</ul>
<section>
</section>
</div>
</div>
<%= link_to 'Edit', edit_loan_path(@loan) %> |
<%= link_to 'Back', loans_path %>
class PaymentsController < ApplicationController
# POST /loans/:loan_id/payments
def create
@loan = Loan.find(params[:loan_id])
@loan.make_payment(payment)
if @load.save
redirect_to @loan, notice: "Payment successful"
else
redirect_to @loan, alert: "Payment count not be processed"
end
end
private
def payment
require(:loan).require(:payment)[:payment]
end
end
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:index, :show]
resources :loans, shallow: :true do
resources :payments, only: :create
end
root 'static_pages#home'
end
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
loan_payments POST /loans/:loan_id/payments(.:format) payments#create
loans GET /loans(.:format) loans#index
POST /loans(.:format) loans#create
new_loan GET /loans/new(.:format) loans#new
edit_loan GET /loans/:id/edit(.:format) loans#edit
loan GET /loans/:id(.:format) loans#show
PATCH /loans/:id(.:format) loans#update
PUT /loans/:id(.:format) loans#update
DELETE /loans/:id(.:format) loans#destroy
root GET / static_pages#home
{"_method"=>"post",
"authenticity_token"=>"VI7rp4N2eHw+tPkY2noD/so9vnGKy/ue06052Vj1f4qidNzgDjZT3aV5v8+XZMs2ZqiUWE0xzAMSQPWltfT6lg==",
"loan_id"=>"5"}
答案 0 :(得分:2)
您在此方法中遇到的问题:
def payment
require(:loan).permit(:payment)[:payment]
end
将其修复为:
def payment
params.permit(:loan_id)
end
阅读如何在Rails中允许params
。
现在我收到错误
ActionController::ParameterMissing in PaymentsController#create param is missing or the value is empty: loan
这是正常行为,因为您的参数为空,除了load_id
之外,您没有向该操作发送任何内容,以发送带有params
属性的@load
,将其传递给{ {1}}助手,如:
loan_payments_path()
并允许它在控制器代码中:
<li><%= link_to "New Payment", loan_payments_path(@loan.id, params: { loan_amount: @loan.amount }), :method => :post %></li>