1

모두 안녕하세요.중첩 된 양식 및 누에 고치. 정의되지 않은 메서드 'label'for nil : NilClass

Showing /app/views/dashboard/courses/_price.html.erb where line #1 raised: 
undefined method `label' for nil:NilClass 
다음

있는 코드 _form.html.erb :

<%= simple_form_for [:dashboard, @course], html: { multipart: true } do |f| %> 

////// 
<%= f.fields_for :prices do |p|%> 
    <%= render 'price', :f => 'prices' %> 
<% end %> 
<%= link_to_add_association 'Add', f, :prices %> 

//////// 

_price.html.erb :

<%= p.label :price %> 
<%= p.text_field :price %> 
<%= p.label :desc %> 
<%= p.text_field :description %> 
<%= link_to_remove_association "remove", f %> 

모델 :

내가 /courses/new (또는 /courses/some_id/edit)를 열 때, 브라우저는이 오류를 반환
class Price < ActiveRecord::Base 
    belongs_to :course 
end 
class Course < ActiveRecord::Base 
has_many :prices 
accepts_nested_attributes_for :prices, :reject_if => :all_blank, :allow_destroy => true 
end 

이 오류를 해결하는 방법? 왜 그것이 발생 했습니까?

+1

당신은'simple_form_for'을 사용하고 있습니다,''%% f.simple_fields_for : prices do | p | %>' – Pavan

+0

고마워요, Pavan. 그것은 작동합니다! – DmitrySharikov

+0

제 제안이나 요시지 씨의 대답과 함께? – Pavan

답변

2

<%= f.fields_for :prices do |p|%> 

<%= f.simple_fields_for :prices do |p|%> 

더 많은 정보에 대한 Git에서보세요해야 당신은 simple_form_for를 사용하는, 그래서 내가이 줄을 것 같아요. 당신이 인수로 전달하지 않았기 때문에 당신의 _price.html.erb 부분보기에서

+0

도움, 고마워요. BTW, Pavan, 보석 고치에 대한 한 가지 문제를 해결하도록 도와 줄 수 있습니까? – DmitrySharikov

+0

@DmitrySharikov 그 보석 문제라면 나는 누에 고치 보석에 대해 아무것도 모르기 때문에 그럴 수 없습니다. – Pavan

+0

이 답변은 질문을 해결하지 못했습니다. 답은 '정의되지 않은 메소드'라벨 인 'nil : NilClass' 오류에 대한 해결책을 제공하지 않습니다. – MrYoshiji

1

이, 당신이 존재하지 않는 폼 빌더 (입니다 nil)를 사용하는 :

# _price.html.erb 
<%= p.label :price %> 
    #^ the variable `p` is the form builder here 

이 문제를 해결하기 위해, 당신은을 통과해야 양식 작성 도구는 다음과 같이 부분보기에 표시됩니다.

<%= f.fields_for :prices do |p| %> 
    <%= render 'price', :f => 'prices', p: p %> 
            #^^^^ We pass the variable `p` (form builder) to the partial 
<% end %> 

희망이 있습니다.

+0

의심의 여지가 있습니다.'fields_for'도'simple_form_for'와 함께 사용할 수 있습니까? – Pavan

+0

나는 자기를 짐작했다. 도와 주셔서 감사합니다! – DmitrySharikov

+0

@Pavan'simple_form_for '는 이미 존재하는'form_for'의 확장입니다. 'simple_fields_for'는 더 많은 옵션을주고 새로운 입력 필드를 정의하기 위해'simple_form_for' 문법을 사용합니다. – MrYoshiji