【问题标题】:Rails 3 - Fields_for Nested attributes not showing on formRails 3 - Fields_for 嵌套属性未显示在表单上
【发布时间】:2012-07-08 21:18:14
【问题描述】:

好吧,这很奇怪,我基本上有以下课程:

class PriceProfile < ActiveRecord::Base
  has_many :prices
  has_many :price_profile_date_ranges

  attr_accessible :name, :price_profile_date_ranges_attributes
  accepts_nested_attributes_for :price_profile_date_ranges

}

class PriceProfileDateRange < ActiveRecord::Base
  attr_accessible :end_date, :price_profile_id, :start_date, :prices, :prices_attributes
  has_many :prices, :dependent=>:destroy
  belongs_to :price_profile

  accepts_nested_attributes_for :prices
}

class Price < ActiveRecord::Base
  attr_accessible :price_profile_date_range_id, :price_profile_id, :product_id, :value
  belongs_to :price_profile
  belongs_to :price_profile_date_range
  belongs_to :product
}

价格配置文件定义了价格随时间变化的特定产品的定价方案。应用价格的日期范围存储在 price_profile_date_range 表中,最后价格表保存所有价格。我正在使用以下控制器和视图在此处创建表单,以便在创建日期范围时设置价格。基本上,表单有一个开始和结束日期字段以及一个网格,即它会有一个针对所有产品的文本框列表来输入价格。

这是视图:

.row
  .span9
    = simple_form_for(@price_profile_date_range, :class=>'well') do |f|


  .form-inputs
    = f.input :start_date, :required => true, :as => :string, :input_html =>{:class=>'datepicker'}
    = f.input :end_date, :required => true, :as => :string, :input_html =>{:class=>'datepicker'}
    = f.input :price_profile_id, :as=>:hidden
    %table.table.table-bordered.table-condensed.table-striped
      %tr
        %td
        - @products.each do |product|
          %td
            =product[:name]
          %td
            - f.fields_for(:prices) do |price_element|
              = price_element.input :value, :class=>'span1'
              = price_element.input :price_profile_id, :as=>:hidden
              = price_element.input :price_profile_date_range_id, :as=>:hidden
              = price_element.input :product_id, :as=>:hidden
  .form-actions
    = f.button :submit

这并不完全是最终形式 - 问题是 f.fields_for 行似乎没有执行。在控制器中,我使用一组价格初始化 @price_profile_date_range 对象。如果我进行加价检查,即使在视图中也会显示所有价格对象,但是 fields_for 根本不执行。我被困在这里真的很糟糕。

【问题讨论】:

    标签: ruby-on-rails-3 haml simple-form


    【解决方案1】:

    尝试将 - 更改为 = - 听起来很傻,但也许这就是问题所在。

    【讨论】:

    • 我遇到了类似的问题,但没有使用haml。我改变了 到 这对我有用。
    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多