2012-07-11 1 views
0

초보자는 프로젝트에 뛰어 들어 레일에 루비를 배우게됩니다. 이 질문은 아마도 순수 루비이며 레일과 관련이 없습니다. 나는 또한 내가 Active_Admin을 사용하고 있음을 주목하고 싶다.클래스 변수 사용

본인에게는 소유자와 전화의 두 가지 수업이 있습니다.

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 

, 당신은에서 Owner를 업데이트 할 수 있는지 확인 Phone 모델은 accepts_nested_attributes_for :ownerPhone 모델에 설정하여 모델을 조정합니다.

0

이 경우 nested form을 사용해야합니다. 중첩 된 형태의 사용은 this railscast에서 매우 잘 설명됩니다.