【问题标题】:undefined method `valid_options' for nil:NilClass with simple_form and Mongoidnil 的未定义方法“valid_options”:具有 simple_form 和 Mongoid 的 NilClass
【发布时间】:2012-05-04 16:23:20
【问题描述】:

我有两个模型,Category 和 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

我正在使用 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):

我该如何解决? 谢谢!

【问题讨论】:

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


    【解决方案1】:

    问题已解决。谢谢卡洛斯安东尼奥达席尔瓦:D。

    您可以在http://groups.google.com/group/plataformatec-simpleform/browse_thread/thread/f384f0445af8468e 中找到修复程序,或者:

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

    谢谢!

    【讨论】:

    • 不要为我工作(可能是因为我需要复选框)。这有效:= f.association :categories, :collection => Category.all, as: :check_boxes
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多