我有以下嵌套形式:
<%= 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。它应该显示对contract_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
您在attr_accessible 中缺少accepts_nested_attributes_for:contrat_lines和:contrat_lines_attributes