问题描述
我有以下的车型基本上指的教训和类别。每节课可以有一个类别,每个类别被嵌入的一课。
类课程
包括Mongoid ::文件 现场:标题:类型=>串
现场:类别,:类型=>串
现场:价格:类型=>整数
现场:描述:类型=>串
现场:USER_ID,:类型=>串
validates_ presence_of:标题
validates_ presence_of:类别
validates_ presence_of:价格
validates_ presence_of:USER_ID validates_numericality_of:价格 attr_accessible:标题,:类别:说明,:价格 embeds_one:类别
结束
类类别
包括Mongoid ::文件
现场:名称,类型:字符串 embedded_in:教训
结束
和我有一个这样的形式:
<%= simple_form_for @lesson,:HTML = GT; {:类=> '好'}做| lesson_form | %GT;
<%如果lesson_form.error_notification%GT;
< DIV CLASS =&GT警报警报错误淡入
<一类=关闭数据解雇=警示的href =#>&安培;倍;< / A>
&所述;%= lesson_form.error_notification%GT;
< / DIV>
<%结束%GT;
<%= lesson_form.input:标题%GT;
<%= lesson_form.input:类别%GT;
<%= lesson_form.input:描述%GT;
<%= lesson_form.input:价格%GT;
<%= lesson_form.association:类别%GT;
<%= lesson_form.button:提交:标签=> 创建,:类=> BTN BTN-主要BTN-大'%GT;
<%结束 - %GT;
当试图渲染的形式,我得到这个错误:
未定义的方法'选项'的#< Mongoid ::关系::元数据:0x000000049dc958>
有什么想法我怎么能显示的类别名称以这种形式?
修改
我已经改变了这一行:<%= lesson_form.association:类别%>
有关这一个:<%= lesson_form.input:类别:收集= GT; Category.all%GT;
但尝试加载我得到的表单时:
访问集合类是不允许的,因为它是一个嵌入式的文档,请从根文件访问的集合。
不幸的是,似乎没有按'科技支撑奥姆斯比其他的ActiveRecord(见)。对不起,这不是一个很好的回答你的问题:/
I have the following models which basically refer to Lessons and Categories. Each lesson can have one category, and each category is embedded in a lesson.
class Lesson
include Mongoid::Document
field :title, :type => String
field :category, :type => String
field :price, :type => Integer
field :description, :type => String
field :user_id, :type => String
validates_presence_of :title
validates_presence_of :category
validates_presence_of :price
validates_presence_of :user_id
validates_numericality_of :price
attr_accessible :title, :category, :description, :price
embeds_one :category
end
class Category
include Mongoid::Document
field :name, type: String
embedded_in :lesson
end
And I have a form like this:
<%= simple_form_for @lesson, :html => { :class => 'well' } do |lesson_form| %>
<% if lesson_form.error_notification %>
<div class="alert alert-error fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<%= lesson_form.error_notification %>
</div>
<% end %>
<%= lesson_form.input :title %>
<%= lesson_form.input :category %>
<%= lesson_form.input :description %>
<%= lesson_form.input :price %>
<%= lesson_form.association :category %>
<%= lesson_form.button :submit, :label => 'Create', :class => 'btn btn-primary btn-large' %>
<% end -%>
When trying to render that form, I get this error:
undefined method `options' for #<Mongoid::Relations::Metadata:0x000000049dc958>
Any thoughts how can I show the categories names in that form?
EDIT:
I have changed this line: <%= lesson_form.association :category %>
For this one: <%= lesson_form.input :category, :collection => Category.all %>
But when trying to load the form I get:
Access to the collection for Category is not allowed since it is an embedded document, please access a collection from the root document.
Unfortunately it appears SimpleForm doesn't support ORMs other than ActiveRecord (see this issue for example). Sorry this isn't a very good 'answer' to your question :/
这篇关于导轨+ MongoID +简单的表单关联:未定义的方法`选择'为#&LT; Mongoid ::关系::元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!