2014-09-04 2 views
1

"add"를 클릭하면 새 레코드를 만들지 않는 레일에 간단한 form_for가 있습니다. Contact라는 중첩 된 리소스가있는 Opportunity라는 모델이 있습니다. 연락처 양식은 문제가있는 양식입니다. 내 양식을 작동하는 비슷한 중첩 된 리소스와 비교했지만 문제를 찾을 수 없었습니다.form_for not working rails 4.0

연락처 컨트롤러 :

보기/연락처/new.html.erb :

<div id="new_contact_form"> 

    <%= form_for ([@opportunity, @opportunity.contacts.new]) do |f| %> 
     <div class="field"> 
      <%= f.label "Contact Info:" %> <br /> 
      <%= f.text_area :first_name %> 
     </div> 

     <div class="actions"> 
      <%= f.submit 'Add' %> 
     </div> 
    <% end %> 

    </div> 
여기
def new 
    @contact = Contact.new 
    end 


    def create 

    @opportunity = Opportunity.find(params[:opportunity_id]) 
    @contact = @opportunity.contacts.new(contact_params) 
    if @contact.save 
     redirect_to @opportunity, notice: 'Contact has been added' 
    else 
     redirect_to @opportunity, alert: 'Unable to add Contact' 
    end 
    end 

def contact_params 
    params.require(:contact).permit(:first_name) 
end 

내가 문제를 데 특정 형태 여기 내 코드입니다

제발 도와주세요 ... 감사합니다 !!!

def new 
    @opportunity = Opportunity.find(params[:opportunity_id]) 
    @contact = @opportunity.contacts.build 
end 

및 양식은 다음과 같습니다 :

+2

을 당신은'new' 행동에 인스턴스 변수 opportunity''@를 정의 할 필요가 렌더링 무엇 인 'new.html.erb'. – vee

답변

1
이 당신의 새로운 액션을 변경

<%= form_for ([@opportunity, @contact]) do |f| %> 
    // form fields 
<% end %> 
+0

안녕하세요 - 나는 그것을 시도하고 폼의 첫 번째 인수는 nil을 포함 할 수 없거나 비어있을 수 있습니다. 코드의 첫 번째 줄이 강조 표시됩니다 ... @mandeep –

+0

@ user1547174이 오류는 양식의 기회 또는 연락처가 없음을 나타냅니다. . 대답에 오타가있었습니다. 업데이트 된 답변을 시도하십시오 – Mandeep

+0

안녕하세요 - 여전히 동일한 오류가 발생하고 있습니다 ... –