本文介绍了带有simple_form和Mongoid的nil:NilClass的未定义方法`valid_options'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个模型,类别和发布.
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
我正在使用 simple_form gem
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 :).
但是如果我使用具有simple_form格式的下一个表单:
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 %>
我遇到下一个错误:
Completed 500 Internal Server Error in 23ms
ActionView::Template::Error (undefined method `valid_options' for nil:NilClass):
如何解决?谢谢!
How can I fix it?Thank you!
推荐答案
问题已解决.谢谢卡洛斯·安东尼奥·达席尔瓦:D.
The problem was fixed. Thank you to Carlos Antonio da Silva :D.
<%= f.input :category, :collection => Category.all, :prompt => "Choose a Category" %>
谢谢!
这篇关于带有simple_form和Mongoid的nil:NilClass的未定义方法`valid_options'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!