Ruby on rails 轨道质量分配
我有一个跟踪SAT辅导课程的网站。学生学习的课程是一系列规则的集合。我有一个模型,用于每个辅导课程,称为“坐”,规则模型称为“规则”。我希望网站管理员能够输入一个坐席日期,然后使用复选框选择哪些“规则”的学生在该坐错了。我有点困惑,我如何创建表单来提取特定的规则,而不向我的规则1、规则2等的现有模型添加属性。我正在使用simple_表单来创建表单 我的坐姿:Ruby on rails 轨道质量分配,ruby-on-rails,models,simple-form,Ruby On Rails,Models,Simple Form,我有一个跟踪SAT辅导课程的网站。学生学习的课程是一系列规则的集合。我有一个模型,用于每个辅导课程,称为“坐”,规则模型称为“规则”。我希望网站管理员能够输入一个坐席日期,然后使用复选框选择哪些“规则”的学生在该坐错了。我有点困惑,我如何创建表单来提取特定的规则,而不向我的规则1、规则2等的现有模型添加属性。我正在使用simple_表单来创建表单 我的坐姿: class Sitting < ActiveRecord::Base attr_accessible :date, :commen
class Sitting < ActiveRecord::Base
attr_accessible :date, :comment, :rule_id, :user_id
validates :date, presence: true
belongs_to :user
has_many :combos
has_many :rules, :through => :combos
end
为了正确更新siting.rule\u id,我是否遗漏了什么?我的日志中出现以下错误:
WARNING: Can't mass-assign protected attributes for Sitting: rule_ids
app/controllers/sittings_controller.rb:27:in `create'
总结一下我们在聊天中得到的信息 首先,您不需要同时使用
attr_accessible
和strong_参数。不久前,我发布了一篇文章,解释了这两种方法的不同之处
您正在运行rails 4,因此应该利用强参数,而不是使用受保护的属性。简而言之,从Gemfile以及所有可访问的attr\u
调用中删除此gem
正如Marian注意到的,您的strong params方法中有一个输入错误,您需要允许规则id
,而不是规则id
rule\u id
列已过时,因为Siting有许多:规则:通过而不是Siting属于:rule
——很可能是旧关联代码的产物
一旦在模型中分配了规则ID,它就会在联接表中创建新的联接模型,从而在给定的SIT和传递的规则之间创建关联 你有什么错误吗?你能添加你的表格代码吗。您能更具体地说明您想要做什么吗?您的规则对象似乎有trigger\u id、hint\u id和question\u id列;在这种情况下,它应该属于那些相关的对象,而不是有许多的对象,你实际上没有问任何问题:-)在你的许可证中,它必须是:rule\u id=>[]
(复数!)为什么你的代码中既有属性可访问的又有强参数?00无论如何,您也需要应用Marian的建议,但您不应该同时需要这两个功能。
class Combo < ActiveRecord::Base
belongs_to :sitting
belongs_to :rule
end
<%= simple_form_for(@sitting, html: { class: "form-horizontal"}) do |f| %>
<%= f.error_notification %>
<% Rule.all.each do |rule| %>
<%= check_box_tag "sitting[rule_ids][]", rule.id, @sitting.rule_ids.include?(rule.id) %> <%= rule.id %>
<% end %>
<div class="form-group">
<%= f.input :comment, as: :text, input_html: { rows: "2", :class => "form-control" }, label: "Comments:" %>
</div>
<div class="form-group">
<%= f.date_select :date, as: :date, label: "Taken Date:" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
def create
@sitting = Sitting.new(sitting_params)
respond_to do |format|
if @sitting.save
format.html { redirect_to @sitting, notice: 'Sitting was successfully created.' }
format.json { render action: 'show', status: :created, location: @sitting }
else
format.html { render action: 'new' }
format.json { render json: @sitting.errors, status: :unprocessable_entity }
end
end
end
def sitting_params
params.require(:sitting).permit(:comment, :date, :user_id, :rule_id => [])
end
WARNING: Can't mass-assign protected attributes for Sitting: rule_ids
app/controllers/sittings_controller.rb:27:in `create'