2009-05-04 2 views
0

Ryan Daigle의 블로그 게시물을 가이드 (http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes)로 사용하여 내 사이트의 중첩 된 개체 양식을 구현하려고합니다. 어떤 이유로 든 중첩 된 양식 필드가 뷰에 나타나지 않습니다. 중첩 된 양식 필드가 나타나는 문제

class Instruction < ActiveRecord::Base 
    has_many :steps 
    accepts_nested_attributes_for :steps 
end 

class Step < ActiveRecord::Base 
    belongs_to :instruction 
end 

<% form_for @instruction do |instruction_form| %> 
    <%= instruction_form.error_messages %> 
    <p> 
    <%= instruction_form.label :title %><br /> 
    <%= instruction_form.text_field :title %> 
    </p> 
    <p> 
    <%= instruction_form.label :difficulty %><br /> 
    <%= instruction_form.text_field :difficulty %> 
    </p> 

<% instruction_form.fields_for :steps do |step_form| %> 
    <%= step_form.label :explanation, 'Explanation: ' %> 
    <%= step_form.text_field :explanation %> 

<% end %> 

    <p><%= instruction_form.submit "Submit" %></p> 
<% end %> 

내가 양식 만 제출시 렌더링 instruction_form.fields_for :steps do |step_form|instruction_form.fields_for :step do |step_form|에 변경

는, 나는 '알 수없는 속성을 : 단계'얻을 오류입니다.

자습서와 일치하는 것으로 보입니다. 무엇을 확인해야합니까? 감사.

답변

2

컨트롤러에서 무슨 일이 벌어지고 있습니까? 튜토리얼을 아직 읽지는 않았지만 지금 당장 (아래로) 당겨 볼 수는 없지만 채우기 위해 메모리에 객체를 구축하고 있습니까? 컨트롤러에서

, 당신의 "새로운"행동에, 당신은

@instruction = Instruction.new 
@instruction.steps.build 

이것은에서 양식을 작성하기위한 "자리"로 메모리에 Step을 인스턴스화되어 있는지 확인하십시오. . . 적어도 이것은 내 컨트롤러에서 accepts_nested_attributes_for을 사용할 때하는 일이며, 훌륭하게 작동합니다.

작동하는지 알려줘, 내가 자습서를 끌어 수 있습니다 일단 난 내가 @instruction = Instruction.new 내가 @instruction을 할 필요가 몰랐 필요하다는 것을 알고이

+0

을 편집 할 수 있습니다 .steps.build. 튜토리얼을 되돌아 보면 '엑스트라'섹션을 읽어야한다는 것을 알았습니다. 도와 주셔서 감사합니다! – sutee

+0

환상적! 그것은 어둠 속의 총 이었지만, 내가 accepts_nested_attributes_for를 사용할 때 알아 차 렸던 것은 이상한 것이었다 환호! – BushyMark