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
类容器<%= 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