2012-07-11 42 views
0

初学者在这里,通过跳入项目学习Ruby on Rails。这个问题可能是纯粹的ruby,与rails没有任何关系。我也想指出我正在使用Active_Admin。使用类变量

我有以下2类:所有者和电话。

class Owner < ActiveRecord::Base 
    attr_accessible :email, :password, 
    has_many :phones 
end 

class Phone < ActiveRecord::Base 
    attr_accessible :owner_id :model 
    belongs_to :owner 
end 

我怎么会去访问该业主从手机模型中的电子邮件,例如:

form do |f| 
    f.inputs "Phone Details" do 
    f.input :model 
    f.input :owner_id # This is where I want the email, not the owners id. 
    end 
    f.buttons 
end 

它看起来像我应该检讨镐头书,跳入轨道之前改进我的红宝石。

感谢

回答

1

兼得的老板和一种形式的手机,你的表格应该是这个样子:

form_for @phone do |phone_form| 
    phone_form.label :model 
    phone_form.text_field :model 
    fields_for @phone.owner do |owner_fields| 
    owner_fields.label :email 
    owner_fields.text_field :email 

如果您使用此方法,确保您可以从更新OwnerPhone型号通过设置accepts_nested_attributes_for :owner您的Phone型号。

0

在这种情况下,您应该使用nested form。嵌套形式的使用很好地解释了railscast中的this