在我的应用程序中,我有一个称为“选择”的多对多关系的对象
INSERT INTO mytab (numcol) VALUES (to_number('2,4', '99999999D99999999'));
和2个彼此相关的对象
class Selection < ActiveRecord::Base
belongs_to :question
belongs_to :projecttype
end
和...
class Projecttype < ActiveRecord::Base
has_many :selections
has_many :questions, :through => :selections
validates :projecttype, uniqueness: true, length: { is: 3 }
validates :name, presence: true
mount_uploader :image, ImageUploader
end
现在,在我的项目类型编辑表单中,我希望能够选择相关问题(使用复选框)。
class Question < ActiveRecord::Base
has_many :selections
has_many :projecttypes, :through => :selections
validates :question, presence: true
validates :comment, presence: true, length: { minimum: 5}
validates :sequence, presence: true
end
尝试了options_for_selection和_from_collection,不再知道该做什么,因为我遇到了很多错误。
<div class="row">
<div class="col-md-2"><%= f.label :selection %></div>
<div class="col-md-4"><%= f.select :selection, {}, {:multiple => true, :style => "width:100%; border:none" } %></div>
</div>
代码应该是什么?
答案 0 :(得分:0)
我现在对Rails有点生疏了,但它应该是这样的。
<%= f.select :selection, Selection.all.collect {|x| x.name}, {}, :multiple => true %>
并且不要忘记添加
def post_params
params.require(:post).permit(:title, :body, category_ids: [])
end
到您的selection.rb文件