0

我有以下嵌套形式:

<%= form_for @contrat,:url => backend_orders_update_report_url(@contrat) do |f| %>
<%= f.fields_for :contrat_lines do |fcl| %>
    <%=  fcl.object.inspect  %>
  <% end %>
<% end %>

输出如下:

nil 

在嵌套表单中,我想显示一些元素,而不是作为表单,而是作为原始文本和一些作为表单字段。通常通过执行 f.object.name 以一种形式,我会访问该名称并能够根据需要显示它。但是,如果我在这里执行 fcl.object,则只有 nil。它应该显示对 contrat_line 对象的检查。

是否可以以嵌套形式访问数据?

编辑 :

控制器动作:

def show_report
  @contrat = Contrat.find(params[:id])
end

以下是模型在开始时的关系:

对比线:

class ContratLine < ActiveRecord::Base
  include Priceable

  belongs_to :contrat
  belongs_to :good
  #a enlever ici et dans la base
  attr_accessible :active_start,:active,:good_id,:pricing,:contrat
  validates :active_start, :presence=> true,:if => "active"
  validate :active_start_smaller_than_active_stop
  validate :active_start_day_cannot_be_greater_than_28
  has_one :pricing, :as => :priceable, :dependent => :delete
  before_validation :convert_month_year_to_date
  after_save :set_user_subscription_date

对比:

class Contrat < ActiveRecord::Base

  has_many :contrat_lines, :dependent => :delete_all

  belongs_to :user
  attr_accessible :user_id,:pricing_id,:state,:adresse_id,:adresse,:payment,:adresse_attributes,:automatic,:start_date,:end_date
  enum_attr :state, %w(basket waiting_data to_confirm paid) do
    labels :basket=>'Panier',  :to_confirm=>'Non payé',:paid=>'Payé'
  end
  enum_attr :payment, %w(debit_card wire_transfer cheque direct_debit)
  belongs_to :adresse
  accepts_nested_attributes_for :adresse, :allow_destroy => true
  scope :by_state,  lambda { |state| where("state = ?",state) }
  scope :last_automatic, where("automatic = true").order("invoice_date DESC")
  scope :last_with_adresse, where("state != 'basket'").order("invoice_date DESC")

  before_validation :set_numbers
4

1 回答 1

1

您在 attr_accessible 中缺少 accept_nested_attributes_for :contrat_lines 和 :contrat_lines_attributes

于 2013-02-10T11:47:04.327 回答