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
Ruby on rails 在rails嵌套属性中编辑联接表值_Ruby On Rails_Ruby On Rails 4_Nested Attributes_Has Many Through - Fatal编程技术网

Ruby on rails 在rails嵌套属性中编辑联接表值

Ruby on rails 在rails嵌套属性中编辑联接表值,ruby-on-rails,ruby-on-rails-4,nested-attributes,has-many-through,Ruby On Rails,Ruby On Rails 4,Nested Attributes,Has Many Through,我已经在这上面呆了一段时间,所以我想我应该把它扔出去 我有两个模型和一个连接模型: class Container < ActiveRecord::Base has_many :theme_containers has_many :themes, :through => :theme_containers end class Theme < ActiveRecord::Base has_many :theme_containers has_many :cont

我已经在这上面呆了一段时间,所以我想我应该把它扔出去

我有两个模型和一个连接模型:

class Container < ActiveRecord::Base
  has_many :theme_containers
  has_many :themes, :through => :theme_containers
end

class Theme < ActiveRecord::Base
  has_many :theme_containers
  has_many :containers, :through => :theme_containers
end

class ThemeContainer < ActiveRecord::Base
  belongs_to :container
  belongs_to :theme
end
类容器:主题\u容器 结束 类主题:主题\u容器 结束 类的子容器 伟大的。我知道这个关联是有效的,因为在控制台中,当我键入Theme.first.containers和Theme.first.Theme\u containers时,我得到了我期望的模型(我已经暂时手动创建了Theme\u容器实例)

问题是,在我的主题表单中,我希望能够更新连接数据(主题容器)上的属性

这是我表格的简化版本:

<%= form_for(@theme) do |f| %>
  <%= f.fields_for :theme_containers do |builder| %>
    <%= render 'theme_container_fields', f: builder %>
  <% end %>
<% end %>

当我运行这个程序时,container_fields partial只渲染一次,而builder对象似乎正在查看原始的@theme对象。我的方法有明显的问题吗?我想做的事可能吗

另外,我运行的是rails 4,所以我没有使用accepts_nested_attributes_for,我已经设置了强参数。我不认为这会影响到我的具体问题,只是把它也扔出去


谢谢

我要做的是:

class Theme < ActiveRecord::Base
  has_many :theme_containers
  has_many :containers, :through => :theme_containers

  accepts_nested_attributes_for :theme_containers
end
有几件事-1。您是否在
新操作中“构建”了
主题容器
对象?2.您是否也在
主题容器
模型中包含
接受
的嵌套属性?
def new
  @theme = Theme.new
  Container.all.each do |container|
    @theme.theme_container.build(container_id: container.id)
  end
end