Rails 4: TypeError 没有将 Symbol 隐式转换为 String

Rails 4: TypeError no implicit conversion of Symbol into String

我是 Rails 的新手,我遇到了一个似乎很常见的问题,但在阅读了所有回复后,我似乎无法为我的情况做任何工作。

我有一个跟踪贷款和用户的应用程序,我正在尝试添加支付功能。当我尝试访问付款页面时,我遇到了这个 TypeError: no implicit conversion of Symbol into String。我尝试了很多东西,但似乎没有任何效果。我不确定我是如何尝试访问 loan_id 或其他内容的。

PaymentsController#create 遇到错误

这是我的观点:

show.html.erb

<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 %>

app/controllers/payments_controller

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"}

你这个方法的问题:

def payment
  require(:loan).permit(:payment)[:payment]
end

修正为:

def payment
  params.permit(:loan_id)
end

阅读如何在 Rails 中允许 params

更新:

Now I get the error ActionController::ParameterMissing in PaymentsController#create param is missing or the value is empty: loan

这是正常行为,因为你的参数是空的,除了 load_id 之外,你没有向操作发送任何内容,要发送具有 @load 属性的 params,将其传递给loan_payments_path() 助手,例如:

  <li><%= link_to "New Payment", loan_payments_path(@loan.id, params: { loan_amount: @loan.amount }), :method => :post %></li>

并在控制器代码中允许它:

def payment
  params.permit(:loan_id, :loan_amount)
end