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 %>
내가 다음 오류를? 감사합니다.
사실 수정 사항이 아니며 해결 방법이 더 많습니다. –
나를 위해 일하지 마십시오 (어쩌면 체크 박스가 필요하기 때문에). 이것은 작동합니다 : = f.association : categories, : collection => Category.all, : : check_boxes –