Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在表单中具有\u多个直通连接表属性_Ruby On Rails_Ruby_Forms_Has Many Through - Fatal编程技术网

Ruby on rails 在表单中具有\u多个直通连接表属性

Ruby on rails 在表单中具有\u多个直通连接表属性,ruby-on-rails,ruby,forms,has-many-through,Ruby On Rails,Ruby,Forms,Has Many Through,我有以下型号: class RandomExam < ActiveRecord::Base has_many :random_exam_sections has_many :sections, :through => :random_exam_sections end class Section < ActiveRecord::Base has_many :random_exam_sections has_many :random_exams, :throug

我有以下型号:

class RandomExam < ActiveRecord::Base
  has_many :random_exam_sections
  has_many :sections, :through => :random_exam_sections
end

class Section < ActiveRecord::Base
  has_many :random_exam_sections
  has_many :random_exams, :through => :random_exam_sections

class RandomExamSection < ActiveRecord::Base
  belongs_to :random_exam
  belongs_to :section
end
结束

我得到一个类型错误:
TypeError:nil既不是符号也不是字符串
当我在方法
assign\u random\u questions\u number

当我在控制台上运行此操作时,甚至会出现此错误

random = RandomExamSection.first
random.update(questions_number: 10)
或者当我跑步时:

random = RandomExamSection.first
random.questions_number = 10
random.save
编辑

我最终删除了RandomExamSection中的关联,并在“assign_random_questions_number”中用问题编号重新创建了它


谢谢。

如果使用嵌套的属性,可以执行以下操作:

#form
<h4>Selected exams</h4>
<%= f.fields_for :random_exam_sections do |b| %>
  <%= b.hidden_field :section_id %>
  <%= b.label :selected, b.object.section.name %>
  <%= b.check_box :selected, { checked: !b.object.id.blank? } %>
  <br>
  <%= b.label :question_numbers %>
  <%= b.text_field :questions_number %>
 <% end %>

#RandomExamModel
class RandomExam < ApplicationRecord
  has_many :random_exam_sections, inverse_of: :random_exam
  has_many :sections, :through => :random_exam_sections

  accepts_nested_attributes_for :random_exam_sections, reject_if: :is_not_selected


  private
  def is_not_selected(attr)
    attr["selected"] == '0'
  end
end

# RandomExam
class RandomExamSection < ApplicationRecord
  belongs_to :random_exam
  belongs_to :section

  attr_accessor :selected
end

# Controller
# GET /random_exams/new
  def new
    @random_exam = RandomExam.new
    @random_exam.random_exam_sections.build(Section.all.map{|s| {section_id: s.id}})
  end
这里的技巧是在控制器上构建,然后在视图上选择,并在模型上验证所选的。这里我举了一个例子,如果你需要帮助:


要在已经创建了随机考试部分时添加部分,您可能应该使用javascript。也许这个railscasts可以帮助您

大家好,欢迎来到stack overflow。因此,我注意到,对于这样多的节,根本没有任何表单字段。您试图在表单中添加什么来实现该功能?如果有,您在尝试该功能时看到了什么错误?@TarynEast嗯,我尝试了几种堆栈溢出解决方案,问题是它们使用嵌套字段,而不是我们在实现中使用的字段。事实上,我们不知道该字段的名称,因为当我们保存它时,它不会出现。所以。。。向我们展示您的尝试(编辑您的问题并将其放在那里),我们可以帮助您修复它,以便它确实出现:)AFAICT它只是另一个字段。。。因此,添加另一个名为what your want的字段。另外-您听说过的
字段吗?对于您的部分,使用它可能更好…@TarynEast我已经更新了答案,现在我在尝试更新联接表记录时遇到了一个类型错误,即使是在控制台上。你知道为什么会发生这种情况吗?这个解决方案很好,唯一的问题是每次我都需要显示所有的复选框,即使是在用户编辑记录的时候!您可以在create上进行选择性构建,以便仅为未选择的部分构建记录。我用了另一种方法,但你的回答很好,谢谢。我将把它标为正确答案。如果你能投票支持这个问题,那就太好了,我想很多人都在为此挣扎。谢谢你抽出时间。
random = RandomExamSection.first
random.questions_number = 10
random.save
#form
<h4>Selected exams</h4>
<%= f.fields_for :random_exam_sections do |b| %>
  <%= b.hidden_field :section_id %>
  <%= b.label :selected, b.object.section.name %>
  <%= b.check_box :selected, { checked: !b.object.id.blank? } %>
  <br>
  <%= b.label :question_numbers %>
  <%= b.text_field :questions_number %>
 <% end %>

#RandomExamModel
class RandomExam < ApplicationRecord
  has_many :random_exam_sections, inverse_of: :random_exam
  has_many :sections, :through => :random_exam_sections

  accepts_nested_attributes_for :random_exam_sections, reject_if: :is_not_selected


  private
  def is_not_selected(attr)
    attr["selected"] == '0'
  end
end

# RandomExam
class RandomExamSection < ApplicationRecord
  belongs_to :random_exam
  belongs_to :section

  attr_accessor :selected
end

# Controller
# GET /random_exams/new
  def new
    @random_exam = RandomExam.new
    @random_exam.random_exam_sections.build(Section.all.map{|s| {section_id: s.id}})
  end
- Build on controller the random_exam_sections to be selected
- Write a form that allows to you 'select' one option and assign the number
- Then, validate if the random_exam_section of a sections was selected (this why i made that `attr_accessor :selected`, i need a place to write if user select the exam_section)
- If was selected, save.