nil的未定义方法`valid_options':带有simple_form和Mongoid的NilClass

时间:2012-05-04 16:23:20

标签: ruby-on-rails ruby-on-rails-3 mongoid simple-form

我有两个型号,分类和邮政。

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 gem

如果我在下一篇文章中写下:

<%= 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格式的下一个表单:

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

我收到了下一个错误:

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

我该如何解决? 谢谢!

1 个答案:

答案 0 :(得分:0)

问题得到解决。谢谢Carlos Antonio da Silva:D。

您可以在http://groups.google.com/group/plataformatec-simpleform/browse_thread/thread/f384f0445af8468e或:

中找到该修复程序
<%= f.input :category, :collection => Category.all, :prompt => "Choose a Category" %>

谢谢!