Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 &引用;无法修改关联,因为源反射类通过以下方式关联:has“u many”;_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails &引用;无法修改关联,因为源反射类通过以下方式关联:has“u many”;

Ruby on rails &引用;无法修改关联,因为源反射类通过以下方式关联:has“u many”;,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我看过其他标题和问题类似的问题,但他们的解决方案似乎不适用于我。对不起,没有更详细的描述,我已经达到了150的上限 我有以下型号: class Event < ActiveRecord::Base has_many :weeks, dependent: :destroy has_many :tests, through: :weeks accepts_nested_attribues_for :weeks, allow_destroy: true accepts_nes

我看过其他标题和问题类似的问题,但他们的解决方案似乎不适用于我。对不起,没有更详细的描述,我已经达到了150的上限


我有以下型号:

class Event < ActiveRecord::Base
  has_many :weeks, dependent: :destroy
  has_many :tests, through: :weeks

  accepts_nested_attribues_for :weeks, allow_destroy: true
  accepts_nested_attribues_for :tests, allow_destroy: true
end

class Week < ActiveRecord::Base
  belongs_to :event
  has_many :tests, dependent: :destroy
end

class Test < ActiveRecord::Base
  belongs_to :week
end
这对于添加事件和周非常有效,但当我尝试添加新测试时,会出现一个错误,显示:

无法修改关联“事件#测试”,因为源反射类“测试”通过以下方式与“周”关联:has#many

我理解这是由于我在视图中获得关联的方式,但我不太确定应该如何使用它们

这是我在上面使用的
link\u to\u add\u字段
helper中的代码:

def link_to_add_fields(name, f, association, parent: 0)
  new_object = f.object.send(association).klass.new
  id = new_object.object_id
  fields = f.fields_for(association, new_object, child_index: id) do |builder|
    render(association.to_s.singularize + "_fields", f: builder, s: parent)
  end
  link_to(name, '#', class: 'btn btn-default add_fields', id: "add_#{association}", data: {id: id, association: association, fields: fields.gsub("\n", "")})
end
助手生成代码,稍后通过JavaScript将这些代码引入表单。这是Railcast一集中使用的一个修改版本

这是我在控制器上用来限制参数的:

def event_params
  params.require(:event).permit(:name, :start, :end, :place,
                                 tests_attributes: [:id, :name, :date, :time, :week_id],
                                 weeks_attributes: [:id, :start, :end, :_destroy])
end
这非常适用于编辑已有测试的数据,但不适用于助手生成的新测试。我读到(在我的例子中)
tests\u attributes
应该在
week\u attributes
之内,因为我试图修改的测试属于week。像这样:

def event_params
  params.require(:event).permit(:name, :start, :end, :place,
     weeks_attributes: [:id, :start, :end, :_destroy, test_attributes: [:id, :name, :date, :time, :week_id]])
end
%h3 Tests

-@weeks.each do |week|
  =e.fields_for :weeks, week do |w|
    %h5=week.start # Prints the 'start' of the Week.
    %table
      %thead
        %th Start
        %th End
        %th X
      %tbody
        = w.fields_for :tests, week.tests do |t|
          =render 'test_fields', t: t, s: week.id
    =link_to_add_fields 'Add Test', w, :tests, parent: week.id
def event_params
  params.require(:event).permit(:name, :start, :end, :place,
     weeks_attributes: [:id, :start, :end, :_destroy, tests_attributes: [:id, :name, :date, :time, :week_id]])
end
我不知道如何制作表单以遵循此关联(或者这是否实际上是一个解决方案)。我知道测试无法修改,因为它是只读的,但我不确定如何在视图中安排关联,以便在更新事件时保存它


非常感谢任何指针。

我认为您应该在这里使用复杂的嵌套表单结构来添加测试<代码>e.fields_for:tests应位于
e.fields_for:weeks
块内,以便正确关联


你可以从这里得到想法

这有点棘手,但我明白了。这是一个修改内容的问题

我通过使用类似这样的东西使它工作:

def event_params
  params.require(:event).permit(:name, :start, :end, :place,
     weeks_attributes: [:id, :start, :end, :_destroy, test_attributes: [:id, :name, :date, :time, :week_id]])
end
%h3 Tests

-@weeks.each do |week|
  =e.fields_for :weeks, week do |w|
    %h5=week.start # Prints the 'start' of the Week.
    %table
      %thead
        %th Start
        %th End
        %th X
      %tbody
        = w.fields_for :tests, week.tests do |t|
          =render 'test_fields', t: t, s: week.id
    =link_to_add_fields 'Add Test', w, :tests, parent: week.id
def event_params
  params.require(:event).permit(:name, :start, :end, :place,
     weeks_attributes: [:id, :start, :end, :_destroy, tests_attributes: [:id, :name, :date, :time, :week_id]])
end
诀窍是使为测试生成的字段位于为一周生成的字段内。因此,首先我添加了
e.fields_for:weeks,week do | w |
,这将指示以下字段属于weeks,然后我使用前面的
每个
循环将数据限制到当前选择的周

接下来,我在
字段中使用了新的
w
关联来进行测试。这将使渲染生成的每个字段的格式为“事件[周属性][周id][测试属性][测试id]”,这是我对控制器中参数的期望

link\u to\u add\u字段
中也可以执行同样的操作,其中我更改了属于事件的
e
关联,即属于周的
w
。这还将改变通过JavaScript生成的新记录的格式,使用我前面描述的格式

最后,我必须更改控制器中预期的格式。我使用的是单数形式的
测试
,它必须更改为复数形式,因为可以在表单中添加多个记录。所以我最终得到了这样的结果:

def event_params
  params.require(:event).permit(:name, :start, :end, :place,
     weeks_attributes: [:id, :start, :end, :_destroy, test_attributes: [:id, :name, :date, :time, :week_id]])
end
%h3 Tests

-@weeks.each do |week|
  =e.fields_for :weeks, week do |w|
    %h5=week.start # Prints the 'start' of the Week.
    %table
      %thead
        %th Start
        %th End
        %th X
      %tbody
        = w.fields_for :tests, week.tests do |t|
          =render 'test_fields', t: t, s: week.id
    =link_to_add_fields 'Add Test', w, :tests, parent: week.id
def event_params
  params.require(:event).permit(:name, :start, :end, :place,
     weeks_attributes: [:id, :start, :end, :_destroy, tests_attributes: [:id, :name, :date, :time, :week_id]])
end
而且它有效