如何在嵌套表单中包含 id?
我有一个嵌套表单,我想在商店模型中包含 city_id,这个 <%= s.hidden_field :city_id, @city.id %>
正确吗?如果是的话,我之后要在控制器中添加什么?如果这不是包含它的正确方法,有人可以告诉我正确的方法吗?非常感谢。
<%= form_for @deal ,:url=>{:action =>"create"} do |c|%>
<%= c.text_field :item_name %>
<%= c.text_field :price %>
<%=c.fields_for :stores do |s| %>
<%=s.text_field :store_name %>
<%= s.hidden_field :city_id, @city.id %>
<%=s.text_field :address %>
<%end%>
<%= c.submit "post"%>
<%end%>
控制器
def create
@city = City.find(session[:city_id])
@[email protected](params[:deal])
if @deal.save
redirect_to @deal
flash[:notice]="successfully created"
else
render 'new'
end
end
模型
class City < ActiveRecord::Base
has_many :stores
has_many :deals
end
class Deal < ActiveRecord::Base
belongs_to :city
has_many :stores ,:through =>:store_deals
has_many :store_deals
end
class StoreDeal < ActiveRecord::Base
belongs_to :store
belongs_to :deal
end
错误
NoMethodError in Deal#new
Showing /home/Deals/app/views/deal/new.html.erb where line #13 raised:
undefined method `merge' for 2:Fixnum
Extracted source (around line #13):
10: <tr><td><label>Price</label></td><td><%= c.text_field :price %></td></tr>
11: <%=c.fields_for :stores do |s| %>
12: <tr><td><label>Store</label></td><td><%=s.text_field :store_name %></td></tr>
13: <%= s.hidden_field :city_id, @city.id %>
14: <tr><td><label>Cross street</label></td><td><%=s.text_field :address %></td></tr>
15: <%end%>
16: <tr><td><%= c.submit "post"%></td></tr>
I have a nested form ,and i want to include city_id in the stores model,is this <%= s.hidden_field :city_id, @city.id %>
correct? If yes what do i add in the controller after that? If it is not a correct way to include it can anybody show me the right way please? Much thanks.
<%= form_for @deal ,:url=>{:action =>"create"} do |c|%>
<%= c.text_field :item_name %>
<%= c.text_field :price %>
<%=c.fields_for :stores do |s| %>
<%=s.text_field :store_name %>
<%= s.hidden_field :city_id, @city.id %>
<%=s.text_field :address %>
<%end%>
<%= c.submit "post"%>
<%end%>
controller
def create
@city = City.find(session[:city_id])
@[email protected](params[:deal])
if @deal.save
redirect_to @deal
flash[:notice]="successfully created"
else
render 'new'
end
end
models
class City < ActiveRecord::Base
has_many :stores
has_many :deals
end
class Deal < ActiveRecord::Base
belongs_to :city
has_many :stores ,:through =>:store_deals
has_many :store_deals
end
class StoreDeal < ActiveRecord::Base
belongs_to :store
belongs_to :deal
end
error
NoMethodError in Deal#new
Showing /home/Deals/app/views/deal/new.html.erb where line #13 raised:
undefined method `merge' for 2:Fixnum
Extracted source (around line #13):
10: <tr><td><label>Price</label></td><td><%= c.text_field :price %></td></tr>
11: <%=c.fields_for :stores do |s| %>
12: <tr><td><label>Store</label></td><td><%=s.text_field :store_name %></td></tr>
13: <%= s.hidden_field :city_id, @city.id %>
14: <tr><td><label>Cross street</label></td><td><%=s.text_field :address %></td></tr>
15: <%end%>
16: <tr><td><%= c.submit "post"%></td></tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,现在我已经更仔细地查看了您的代码并看到了问题所在。
hidden_field
帮助器没有value
作为其参数之一。使用:但是,由于迈克尔·杜兰特(Michael Durrant)指出的原因,最好在控制器中处理这个问题。
Okay, now that I've looked over your code more carefully and seen the it's clear what the problem is. The
hidden_field
helper doesn't havevalue
as one of its arguments. Use:However, for the reason Michael Durrant points out, it would be better to handle this in your controller.