【发布时间】:2015-12-26 23:26:15
【问题描述】:
我有三个模型:
class Question < ActiveRecord::Base
has_many :factor_questions
has_many :bigfivefactors, through: :factor_questions
accepts_nested_attributes_for :factor_questions
accepts_nested_attributes_for :bigfivefactors
end
class Bigfivefactor < ActiveRecord::Base
has_many :factor_questions
has_many :questions, through: :factor_questions
end
还有我的连接表,它不仅包含bigfivefactor_id 和question_id,还包含另一个整数列value。
class FactorQuestion < ActiveRecord::Base
belongs_to :bigfivefactor
belongs_to :question
end
创建一个新的Question 工作正常,在我的_form.html.erb 中使用
<%= form_for(@question) do |f| %>
<div class="field">
<%= f.label :questiontext %><br>
<%= f.text_field :questiontext %>
</div>
<%= f.collection_check_boxes :bigfivefactor_ids, Bigfivefactor.all, :id, :name do |cb| %>
<p><%= cb.check_box + cb.text %></p>
<% end %>
这让我可以根据需要勾选或取消勾选bigfivefactors。
但是,正如我之前提到的,连接模型还包含一个value。
问题:
如何在每个复选框旁边添加一个文本字段以即时添加/编辑“值”?
For better understanding, i added an image
在控制台中,我基本上可以做到这一点:
q= Question.create(questiontext: "A new Question")
b5 = Bigfivefactor.create(name: "Neuroticism")
q.bigfivefactors << FactorQuestion.create(question: q, bigfivefactor: b5, value: 10)
我还发现要编辑我的questions_controller:
def new
@question = Question.new
@question.factor_questions.build
end
但我不知道如何将其纳入我的观点。
非常感谢您的帮助!
【问题讨论】:
标签: ruby-on-rails ruby activerecord associations