以嵌套形式访问对象

时间:2013-02-10 11:15:27

标签: ruby-on-rails-3 nested-forms

我有以下嵌套表格:

<%= 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,那么只有零。它应该显示contrat_line对象的检查。

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

编辑:

控制器动作:

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

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

ContratLine:

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

1 个答案:

答案 0 :(得分:1)

你缺少一个accepts_nested_attributes_for:contrat_lines以及:attr_accessible中的contrat_lines_attributes