Ruby on rails nil的未定义方法“贷款金额”:NilClass
我正在使用ruby 2.2.2p95 2015-04-13修订版50295[i686 linux]和Rails 4.2.1进行珠宝贷款申请,因为我有两种型号: 1jewelloan.rb 2jltransaction.rb 我已添加外键jewelloan\u id以获取jltransactions表中的贷款金额。其中,as loan_amount是贷款表中的一个字段 我的问题是外键关系不起作用。我已经搜索了许多stackoverflow问题来解决这个问题,但这些问题都不起作用 我已经附上了我的模型,视图,控制器和一切。请告诉我哪里出错了 jewelloan.rb 请给出一些使用外键的想法Ruby on rails nil的未定义方法“贷款金额”:NilClass,ruby-on-rails,ruby-on-rails-4,activerecord,foreign-keys,foreign-key-relationship,Ruby On Rails,Ruby On Rails 4,Activerecord,Foreign Keys,Foreign Key Relationship,我正在使用ruby 2.2.2p95 2015-04-13修订版50295[i686 linux]和Rails 4.2.1进行珠宝贷款申请,因为我有两种型号: 1jewelloan.rb 2jltransaction.rb 我已添加外键jewelloan\u id以获取jltransactions表中的贷款金额。其中,as loan_amount是贷款表中的一个字段 我的问题是外键关系不起作用。我已经搜索了许多stackoverflow问题来解决这个问题,但这些问题都不起作用 我已经附上了我的模
谢谢…您的关系可能返回零结果。 试一试 你可以试试这个:
<%= jlt.text_field :loan_amount, :disabled => true, :value => @jltransaction.jewelloan.try(:loan_amount) if @jltransaction && @jltransaction.jewelloan %>
现在显示了该字段,但无法获取贷款金额。您是否确保@jltransaction对象具有与之关联的jewelloan记录?我的jltransactions表中的字段贷款金额仍然为空值。您是否可以粘贴@jltransaction.jewelloan?jewelloan加载0.3ms的结果从jewelloans中选择jewelloans.*其中jewelloans.id=?LIMIT 1[[id,4]]在2015-05-29 14:56:02+0530开始获取127.0.0.1的/jltransactions/new?id=4,由jltransactioncontrollernew作为HTML参数处理:{id=>4}在我尝试之后:由jltransactioncontrollercreate作为HTML jltransaction=>{jewelloan\u id=>}2146ms ActiveRecord中已完成500个内部服务器错误:104.7ms NameError未定义的局部变量或方法jewelloan_参数用于:app/controllers/jltransactions_controller.rb:11:in create'在jltransactions_控制器中添加强参数,如jewelloan_属性:[属性….]在jltransactions_控制器中:@jewelloan=@jltransaction.jewelloansjewelloan_参数此行正确吗?添加了强参数,但结果仍然是jltransactions_控制器中相同的强参数,它应该是多个jewelloans_属性:[属性….]
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
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
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
<%= 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 %>
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 %>