undefined method `valid_options' for nil:NilCl

2019-07-19 17:10发布

问题:

I have two models, Category and Post.

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

I'm using simple_form gem

If I write in my post form the next:

<%= 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 %>

The form does works fine :).

but if I use the next form with simple_form format:

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

I get the next error:

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

How can I fix it? Thank you!

回答1:

The problem was fixed. Thank you to Carlos Antonio da Silva :D.

you can find the fix in http://groups.google.com/group/plataformatec-simpleform/browse_thread/thread/f384f0445af8468e or:

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

Thank you!