ネストされたフォームがあり、storesモデルに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])
@deal=@city.deals.build(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>