首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails中的嵌套表单没有可见的字段

Ruby on Rails中的嵌套表单没有可见的字段
EN

Stack Overflow用户
提问于 2013-02-06 07:01:29
回答 1查看 254关注 0票数 0

我正在尝试为具有多个账单地址和邮政addresses.When的客户配置文件创建嵌套组。我浏览到客户配置文件表单,看到客户配置文件表单的字段,但没有看到账单地址表单或邮政地址表单的任何字段。有什么想法吗?下面是我的控制器,模型和视图。

我们的目标是有一个客户配置文件,可以多个帐单地址和多个邮政地址。

客户资料模型:

代码语言:javascript
复制
    class CustomerProfile < ActiveRecord::Base
        #These entries are required to create a nested model form (multiple models in one form)
        validates_presence_of :customerNumber
        validates_uniqueness_of :customerNumber


        has_many :billing_addresses, :dependent => :destroy
        has_many :postal_addresses,  :dependent => :destroy

        accepts_nested_attributes_for :billing_addresses, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
        accepts_nested_attributes_for :postal_addresses, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
    end

计费地址模型:

代码语言:javascript
复制
    class BillingAddress < ActiveRecord::Base
        belongs_to :customer_profile
        attr_protected :customerNumber
    end

邮寄地址模型:

代码语言:javascript
复制
    class PostalAddress < ActiveRecord::Base
        belongs_to :customer_profile
        attr_protected :customerNumber
    end

客户配置文件控制器:

代码语言:javascript
复制
    def new
        @customer_profile = CustomerProfile.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @customer_profile }
        end
    end


    def create
        @customer_profile = CustomerProfile.new(params[:customer_profile])

        respond_to do |format|
          if @customer_profile.save
            format.html { redirect_to @customer_profile, notice: 'Customer profile was successfully created.' }
            format.json { render json: @customer_profile, status: :created, location: @customer_profile }
          else
            format.html { render action: "new" }
            format.json { render json: @customer_profile.errors, status: :unprocessable_entity }
          end
        end
    end

客户简档表单:

代码语言:javascript
复制
    <br />
    <h3>Add new customer profile with Billing and Postal Address</h3>
    <br />
    <h4> Customer Profile </h4>
    <%= form_for @customer_profile do |f| %>
        <div class="field">
            <%= f.label :customerNumber %><br />
            <%= f.text_field :customerNumber %>
        </div>

        #.... a few other fields removed to keep this short

    <br />
        <h4> Billing Address </h4>
        <%= f.fields_for :billing_addresses do |b| %>

            <div class="field">
                <%= b.label :addressLine1 %><br />
                <%= b.text_field :addressLine1 %>
            </div>

        #.... a few other fields removed to keep this short

        <% end %>

        <br />
        <h4> Postal Address </h4>
        <br />
        <%= f.fields_for :postal_addresses do |p| %>

            <div class="field">
                <%= p.label :addressLine1 %><br />
                <%= p.text_field :addressLine1 %>
            </div>

        #.... a few other fields removed to keep this short

        <br />
        <% end %>
        <%= f.submit %>
    <% end %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-06 07:45:11

fields_for将显示billing_addresses字段,这些字段是customer_profile的子项。如果这是一个新对象,您需要添加一个或多个,或者通过JavaScript添加适当的字段。您可能希望两者都添加,这样当它们到达那里时会有一个空字段,然后它们可以添加更多字段。

您可以创建一个@customer_profile.billing_addresses.build,使其在呈现表单时为空。

你知道如何通过javascript添加和删除字段吗?它在铁路广播中得到了很好的报道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14718617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档