我有一个嵌套的表单,使用Rails 4.2.5上的'nested_form'gem,和 我遇到了错误:
Invalid association. Make sure that accepts_nested_attributes_for
is used for :monthly_charges association.
我看过这些帖子:
Invalid association. Make sure that accepts_nested_attributes_for is used for :mark_images association
Invalid association. Make sure that accepts_nested_attributes_for is used for :questions association
Rails nested_form Invalid association. Make sure that accepts_nested_attributes_for is used for assoctiation
......根据答案,我看不出我做错了什么。
据我所知,我的模型中设置了正确的关联:
class Lease < ActiveRecord::Base
has_many :monthly_charges
accepts_nested_attributes_for :monthly_charges, allow_destroy: true
end
...我已经宣布了monthly_charges_attributes
字段
控制器中的lease_params
:
def lease_params
params.require(:lease).permit(
:begin_date, :end_date, :renewal_date,
:move_out_date, :rent, :deposit_required, :late_day,
:late_fee, :extra_day_late_fee,
monthly_charges_attributes: [:id, :name, :_destroy]
)
end
在视图中,我有:
<p><%= f.link_to_add "Add a monthly charge", :monthly_charges %></p>
我不知道为什么这不起作用。
更新:目前MonthlyCharge
模型:
class MonthlyCharge < ActiveRecord::Base
belongs_to :lease
end