首页 文章

使用带有has_many的嵌套表单 - 无法指定质量属性

提问于
浏览
2

我在这里几乎每个问题都读过有关has_many通过关联的嵌套表单,但我无法让我的模型起作用 . 有人可以帮忙吗?

有两种模式:原型和裙子偏好,通过裙子模型连接 .

以下是模型:

类Archetype <ActiveRecord :: Base attr_accessible:occasion,:skirt_partworth,:title,:skirtpreferencings_attributes has_many:skirtpreferencings has_many:SkirtPreferences,:through =>:skirtpreferencings accepts_nested_attributes_for:SkirtPreferences accepts_nested_attributes_for:skirtpreferencings end Blockquote class Skirtpreferencing <ActiveRecord :: Base attr_accessible: archetype_id,:skirt_preference_id,:skirtpreferencing_attributes belongs_to:archetype belongs_to:SkirtPreferences accepts_nested_attributes_for:SkirtPreferences end class SkirtPreference <ActiveRecord :: Base attr_accessible:archetype_id,.... has_many:skirtpreferencings has_many:archetypes,:through =>:skirtpreferencings end

表单看起来像这样,显示就好了:

<%= form_for(@archetype)do | f | %> ... <%= f.fields_for:skirtpreferencing do | preference_builder | %> <%= preference_builder.fields_for:SkirtPreferences do | builder | %> <%=渲染“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

1 回答

  • 1

    我猜这是SkirtPreference和Archetype之间的多对多关系,而SkirtPreferencing是SkirtPreference和Archetype之间的关联?

    尝试从 skirtpreferencing_attributes 更改为 skirtpreferences_attributes . 那是我的预感 . 因为您正在尝试将数据添加到skritpreferences,而不是裙子首选项,它们只是在裙子偏好和原型之间关联 .

    我也认为这很不寻常 .

    has_many :SkirtPreference, :through => :skirtpreferencings

    accepts_nested_attributes_for :SkirtPreference

    ...

    belongs_to :SkirtPreference

    accepts_nested_attributes_for :SkirtPreference

    所有这些通常都应该是 :skirtpreferences .


    **编辑1 **

    假设表单是在ArchetypesController中的新操作中生成的...

    您似乎缺少从原型构建一个裙子首选属性的部分 .

    所以你在ArchetypesController中的新动作

    def new
      @archetype = Archetype.new
      @archetype.skirtpreferencings.build
      ...
    end
    

    **编辑2 **

    除类名外, SkirtPreferences 应更改为 skirtpreferences .

    你能尝试将 f.fields_for :skirtpreferencing do 更改为 f.fields_for :skirtpreferencings do

相关问题