如何在嵌套表单中包含 id?

发布于 2024-12-09 15:36:25 字数 2148 浏览 0 评论 0原文

我有一个嵌套表单,我想在商店模型中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-12-16 15:36:25

好的,现在我已经更仔细地查看了您的代码并看到了问题所在。 hidden_​​field 帮助器没有 value 作为其参数之一。使用:

<%= s.hidden_field :city_id, :value => @city.id %>

但是,由于迈克尔·杜兰特(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 have value as one of its arguments. Use:

<%= s.hidden_field :city_id, :value => @city.id %>

However, for the reason Michael Durrant points out, it would be better to handle this in your controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文