我已经在这里阅读了几乎所有关于 has_many 通过关联的嵌套表单的问题,但我无法让我的模型工作。有人可以帮忙吗?
有 2 个模型:原型和裙子偏好,通过裙子偏好模型链接。
以下是模型:
class Archetype < ActiveRecord::Base attr_accessible :occasion, :skirt_partworth, :title, :skirtpreferencings_attributes
has_many :skirtpreferences has_many :SkirtPreferences, :through => :skirtpreferences accept_nested_attributes_for :SkirtPreferences accept_nested_attributes_for :skirtpreferences end
块引用
类 Skirtpreferencing < ActiveRecord::Base attr_accessible :archetype_id, :skirt_preference_id, :skirtpreferencing_attributes
belongs_to :archetype belongs_to :SkirtPreferences 接受_nested_attributes_for
:SkirtPreferences结尾
类 SkirtPreference < ActiveRecord::Base attr_accessible :archetype_id, ....
has_many :skirtpreferences has_many :archetypes, :through => :skirtpreferences
结尾
表单看起来像这样,并且显示得很好:
<%= form_for(@archetype) 做 |f| %> ... <%= f.fields_for :skirtpreferencing do |preference_builder| %> <%= preference_builder.fields_for :SkirtPreferences do |builder| %> <%= render "skirt_preferences_field", :f => builder %> <% end %> <% end %> ...
我想我必须在控制器中做一些事情,但我不确定到底是什么。
谢谢!
添加控制器:
class ArchetypesController < ApplicationController
def new
@archetype = Archetype.new
@archetype.skirtpreferencings.build
end
# GET /archetypes/1/edit
def edit
@archetype = Archetype.find(params[:id])
end
def create
@archetype = Archetype.new(params[:archetype])
end
class SkirtPreferencesController < ApplicationController
def new
@skirt_preference = SkirtPreference.new
end
def edit
@skirt_preference = SkirtPreference.find(params[:id])
end
def create
@skirt_preference = SkirtPreference.new(params[:skirt_preference])
end