Rails 3:使用“belongs_to”关联呈现的嵌套表单

发布于 2024-11-28 19:53:38 字数 2053 浏览 0 评论 0原文

我正在努力获取使用“belongs_to”关联呈现的嵌套表单。

我原本期望“_form.html.erb”(如下)中的地址字段能够正确呈现,但显然事实并非如此,我只是无法理解它的原因!

Rails 版本:3.09

这是代码。

模型:

Store.rb

class Store < ActiveRecord::Base
  has_and_belongs_to_many :products
  belongs_to :store_address, :foreign_key => "address_id", :class_name => "Address"
  ......

end

地址没有任何对 Store 模型的引用(它是独立的)

控制器

stores_controller.rb

  def new
    @store = Store.new
    @store.build_store_address
    respond_with(@store)
  end

查看

new.html。 erb

<% form_for(@store, :url => collection_url) do |f| %>
  <%= render :partial => "form", :locals => { :f => f } %>
  <p class="form-buttons">
    <%= button t('continue') %>

  </p>
<% end %>

_form.html.erb

<%=t(:store_name)%> : <%= text_field :store, :name %>
<%=t(:store_admin_email)%> : <%= text_field :store, :admin_email %>

<fieldset> 
    <legend><label><%=t(:address)%></label></legend> 
    <% f.fields_for :store_address do |address_form| %>
    <table> 
        <tbody><tr> 
            <td width="200"><label><%=t(:line_1)%></label></td><td> 
            <%= address_form.text_area :address1, :rows => 2%> 
            </td> 
        </tr> 
        <tr> 
            <td><label><%=t(:line_2)%></label></td><td> 
            <%= address_form.text_area :address2, :rows => 2 %>  
            </td> 
        </tr> 
        <tr> 
            <td><label><%=t(:city)%></label></td><td> 

            <%= address_form.text_field :city %> 

            </td> 
        </tr> 
.......

这呈现商店名称。但地址方面没有任何内容。请帮忙!

I am struggling to get the nested form rendered with belongs_to association.

I was expecting the address fields in the "_form.html.erb" (below) to be rendered correct, but apparently it is not, and i just cant wrap my head around it to figure why!

Rails version: 3.09

here is the code.

Model:

Store.rb

class Store < ActiveRecord::Base
  has_and_belongs_to_many :products
  belongs_to :store_address, :foreign_key => "address_id", :class_name => "Address"
  ......

end

Address doesn't have any reference to Store model (it is independent)

Controller

stores_controller.rb

  def new
    @store = Store.new
    @store.build_store_address
    respond_with(@store)
  end

View

new.html.erb

<% form_for(@store, :url => collection_url) do |f| %>
  <%= render :partial => "form", :locals => { :f => f } %>
  <p class="form-buttons">
    <%= button t('continue') %>

  </p>
<% end %>

_form.html.erb

<%=t(:store_name)%> : <%= text_field :store, :name %>
<%=t(:store_admin_email)%> : <%= text_field :store, :admin_email %>

<fieldset> 
    <legend><label><%=t(:address)%></label></legend> 
    <% f.fields_for :store_address do |address_form| %>
    <table> 
        <tbody><tr> 
            <td width="200"><label><%=t(:line_1)%></label></td><td> 
            <%= address_form.text_area :address1, :rows => 2%> 
            </td> 
        </tr> 
        <tr> 
            <td><label><%=t(:line_2)%></label></td><td> 
            <%= address_form.text_area :address2, :rows => 2 %>  
            </td> 
        </tr> 
        <tr> 
            <td><label><%=t(:city)%></label></td><td> 

            <%= address_form.text_field :city %> 

            </td> 
        </tr> 
.......

This renders the store name. but nothing on the address side. please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ゃ懵逼小萝莉 2024-12-05 19:53:38

我认为您错过了 <% f.fields_for ... 处的 =。它应该是 <%= f.fields_for...

过去,这种情况经常发生在我身上,错过了这一件事,然后想知道为什么嵌套表单不会呈现。

I think you missed an = at <% f.fields_for .... It should be <%= f.fields_for... .

This has happened to me so often in the past, missing this one thing, and then wondering why the nested form would not render.

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