nil:NilClass 的未定义方法“loan_amount”

undefined method `loan_amount' for nil:NilClass

我正在使用 ruby 2.2.2p95 (2015-04-13 revision 50295) [i686-linux]Rails 4.2.1 做一个 jewel loan 应用程序,因为我有两个模型:

1)jewelloan.rb 2)jltransaction.rb

我添加了外键 jewelloan_id 以在 jltransactions table 中获取 loan_amountloan_amount 是 jeweloans table.

中的一个字段

我的问题是外键关系不起作用。我已经搜索了很多 Whosebug 问题来解决这个问题,但都没有用。

我已经附上了我的模型、视图、控制器和所有东西。请告诉我哪里做错了。

jewelloan.rb

  class Jewelloan < ActiveRecord::Base

    attr_accessor :transaction_amount

    attr_accessible :transaction_amount

    has_many :jltransactions, :dependent => :destroy

    accepts_nested_attributes_for :jltransactions ,:allow_destroy => true

    attr_accessible :account_number, :customer_name, :customer_address, :opened_on, :due_date, :amount, :jewel, :no_of_items, :gross_weight, :net_weight, :appraised_amount, :loan_amount, :transaction_mode, :transaction_type, :particulars, :comments, :close_date, :jltransactions_attributes

 end

jltransaction.rb

 class Jltransaction < ActiveRecord::Base

    attr_accessor :loan_amount

    attr_accessible :loan_amount

    belongs_to :jewelloan 

    attr_accessible :transaction_date, :transaction_amount, :transaction_mode, :transaction_type, :particulars, :comments, :jewelloan_id

 end

jewelloans_controller.rb

 class JewelloansController < ApplicationController

  def new
    @jewelloan = Jewelloan.new
  end

  def create
    @jewelloan = Jewelloan.create(jewelloan_params)
    if @jewelloan.save
      flash[:success] = "Special JL was successfully created"
      redirect_to @jewelloan
    else
      flash[:danger] = "Special Jewel Loan was not created"
      render 'new'
    end
  end

  private

    def jewelloan_params
      params.require(:jewelloan).permit(:account_number, :customer_name, :customer_address, :amount, :interest_rate, :opened_on, :due_date, :amount_due, :jewel, :no_of_items, :gross_weight, :net_weight, :appraised_amount, :loan_amount, :transaction_mode, :transaction_type, :particulars, :comments, :close_date, :no_of_days, :jltransactions_attributes)
    end

  end

jltransactions_controller.rb

 class JltransactionsController < ApplicationController

   def new    
     @jltransaction = Jltransaction.new
     @jewelloan = Jewelloan.new
   end

   def create
     @jltransaction = Jltransaction.create(jltransaction_params)  
     @jewelloan = @jltransaction.jewelloans(jewelloan_params)  
     if @jltransaction.save
       flash[:success] = "Transaction created"
       redirect_to @jltransaction
     else
       flash[:danger] = "Transaction was not created"
       render 'new'
     end
   end

 private
  def jltransaction_params
    params.require(:jltransaction).permit(:transaction_date, :transaction_amount, :transaction_mode, :transaction_type, :particulars, :comments, :jewelloan_id)
  end

 end

app/views/jltransactions/_form.html.erb

 <%= form_for @jltransaction do |jlt| %>

 <%= jlt.hidden_field :jewelloan_id, :value => @jewelloan.id %>

 <% if @jltransaction.errors.any? %>

 <h2>Transaction Failed</h2>
 <ul>

 <% @jltransaction.errors.full_messages.each do |error| %>

 <li><%= error %></li>

 <% end %>

 </ul>

 <% end %>    

 <%= jlt.label :loan_amount %>
 for<%= jlt.text_field :loan_amount, :disabled => true, :value => @jltransaction.jewelloan.try(:loan_amount) %>

 <%= jlt.label :transaction_amount %>
 <%= jlt.text_field :transaction_amount %>

 <%= jlt.submit "Submit Transaction", class: "btn btn-success" %> 

  <% end %>

rails 控制台

 Completed 500 Internal Server Error in 34ms (ActiveRecord: 0.0ms)

 ActionView::Template::Error (undefined method `loan_amount' for nil:NilClass):

请提供一些处理外键的想法。

谢谢...

您的关系可能返回零结果。 尝试

@jltransaction.jewelloan.try(:loan_amount)

你可以试试这个:

<%= jlt.text_field :loan_amount, :disabled => true, :value => @jltransaction.jewelloan.try(:loan_amount) if @jltransaction && @jltransaction.jewelloan %>