2012-05-04 2 views
2

두 가지 모델, 범주 및 게시물이 있습니다.정의되지 않은 메소드 'valid_options'for nil : simple_form 및 Mongoid가있는 NilClass

: 나는 내 게시물에 작성하는 경우 다음을 형성 simple_form gem

을 사용하고

Category.rb

class Category 
include Mongoid::Document 

field :title, :type => String 
has_many :posts, :autosave => true, dependent: :destroy 

end 

Post.rb

class Post 
include Mongoid::Document 

field :title, :type => String 
belongs_to :category 
end 

<%= simple_form_for(@post) do |f| %> 
<%= f.collection_select :category, Category.all, :id, :title, :prompt => "Choose a Category"%> 
<%= f.input :title %> 
<%= f.button :submit %> 
<% end %> 

양식이 제대로 작동합니다. :).

하지만 simple_form 형식과 다음 양식을 사용하는 경우 :

Completed 500 Internal Server Error in 23ms 
ActionView::Template::Error (undefined method `valid_options' for nil:NilClass): 

내가 그것을 어떻게 해결할 수 :
<%= simple_form_for(@post) do |f| %> 
    <%= f.association :category, :prompt => "Choose a Category" %> 
    <%= f.input :title %> 
    <%= f.button :submit %> 
<% end %> 

내가 다음 오류를? 감사합니다.

답변

0

문제가 해결되었습니다. 카를로스 안토니오 다 실바에게 감사드립니다. D.

당신은 http://groups.google.com/group/plataformatec-simpleform/browse_thread/thread/f384f0445af8468e에 수정을 찾거나 할 수 있습니다

<%= f.input :category, :collection => Category.all, :prompt => "Choose a Category" %> 

감사합니다!

+0

사실 수정 사항이 아니며 해결 방법이 더 많습니다. –

+0

나를 위해 일하지 마십시오 (어쩌면 체크 박스가 필요하기 때문에). 이것은 작동합니다 : = f.association : categories, : collection => Category.all, : : check_boxes –

관련 문제