undefined method `valid_options' for nil:NilClass with simple_form and Mongoid

自古美人都是妖i 提交于 2020-01-15 05:40:27

问题


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!



来源:https://stackoverflow.com/questions/10452615/undefined-method-valid-options-for-nilnilclass-with-simple-form-and-mongoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!