通过 link_to 命令传递要在 model#new 页面中引用的对象
我正在创建一个基本的会员卡应用程序,其中包含模型 Merchant 和 Loyaltycard。在merchant#show 视图中,我有这一行,
<%=link_to 'New Loyalty card', new_loyaltycard_path(:merchant_id=>1) %>
我试图将商家ID 传递到loyaltycard#new 视图,以便它会被自动选为该卡的商家。在loyaltycard#_form(由loyaltycard#new调用)中,我有以下几行
<%if @merchant%>
<%= f.hidden_field :merchant_id, :value => @merchant.id %>
<%else%>
<div class="field">
<%= f.label :merchant_id %><br />
<%= f.text_field :merchant_id %>
</div>
<%end%>
,但我不断收到错误消息,提示无法调用Nil类的id。有更好的方法吗?
这是忠诚卡的控制器代码
def new
@loyaltycard = Loyaltycard.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @loyaltycard }
end
end
# GET /loyaltycards/1/edit
def edit
@loyaltycard = Loyaltycard.find(params[:id])
end
# POST /loyaltycards
# POST /loyaltycards.json
def create
@loyaltycard = Loyaltycard.new(params[:loyaltycard])
respond_to do |format|
if @loyaltycard.save
format.html { redirect_to @loyaltycard, notice: 'Loyaltycard was successfully created.' }
format.json { render json: @loyaltycard, status: :created, location: @loyaltycard }
else
format.html { render action: "new" }
format.json { render json: @loyaltycard.errors, status: :unprocessable_entity }
end
end
end
错误是
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
I'm creating a basic loyalty card application, with model Merchant and Loyaltycard. In the merchant#show view I have this line
<%=link_to 'New Loyalty card', new_loyaltycard_path(:merchant_id=>1) %>
I'm trying to pass the merchant ID to the loyaltycard#new view so it will be automatically selected as the merchant for that card. In loyaltycard#_form (which gets called by loyaltycard#new) I have the lines
<%if @merchant%>
<%= f.hidden_field :merchant_id, :value => @merchant.id %>
<%else%>
<div class="field">
<%= f.label :merchant_id %><br />
<%= f.text_field :merchant_id %>
</div>
<%end%>
But I keep getting and error that says can't call id for class Nil. Is there a better way of doing this?
Here is the controller code for loyaltycard
def new
@loyaltycard = Loyaltycard.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @loyaltycard }
end
end
# GET /loyaltycards/1/edit
def edit
@loyaltycard = Loyaltycard.find(params[:id])
end
# POST /loyaltycards
# POST /loyaltycards.json
def create
@loyaltycard = Loyaltycard.new(params[:loyaltycard])
respond_to do |format|
if @loyaltycard.save
format.html { redirect_to @loyaltycard, notice: 'Loyaltycard was successfully created.' }
format.json { render json: @loyaltycard, status: :created, location: @loyaltycard }
else
format.html { render action: "new" }
format.json { render json: @loyaltycard.errors, status: :unprocessable_entity }
end
end
end
The error is
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
new
操作中执行的操作是使用params[:merchant_id]
查找并设置@merchant
然后您的代码应该可以工作,如果没有这个,@merchant 就是 nil,并且你不能在
nil
上调用方法:id
What you need to do in your
new
action is using theparams[:merchant_id]
to look up and set@merchant
Then your code should work, without that, @merchant is nil, and you can't call the method
:id
onnil
您没有在控制器中的任何位置设置变量@merchant,但您在视图中使用它。
You're not setting variable @merchant anywhere in your controller, but you use it the view.