Rails 3 - 设计嵌套表单,给出质量分配错误

时间:2011-01-10 11:22:59

标签: ruby-on-rails ruby-on-rails-3

我知道这已经被覆盖了一段时间,但我似乎无法解决这个错误 -

警告:无法批量分配受保护的属性:new_order_attributes

这是试图保存的嵌套哈希 -

Parameters: {"utf8"=>"✓","authenticity_token"=>"UNKZf7zvlyReHSCbMRRl+9y+F5/2YF8Rf64Wm9O9xyo=", "user"=>{ "new_orders_attributes"=>[{"plan_id"=>"2", "price_id"=>"2222"}], "first_name"=>"Alex", "last_name"=>"Handley", "email"=>"alex@s.co.uk", "job_title"=>"Programmer",  "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}

保存用户数据,但不保存订单。

模型

用户

devise :database_authenticatable,
     :recoverable, :rememberable, :trackable, :validatable

attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :location, :country, :job_title, :company, :parent_id,:first_name,:last_name,:subscription_type, 
:orders, :plan_id, :user_id, :price_id

has_many :orders
has_many :plans, :through => :orders
accepts_nested_attributes_for :orders, :plans

订单

class Order < ActiveRecord::Base
 belongs_to :plan
 belongs_to :user
end

计划

 has_many :orders
 has_many :users, :through => :orders

Rails -v - 3.0.3

嵌套表格

  <% prefix = "user[new_orders_attributes][]" %>
  <%= fields_for prefix, @user.orders do |order_form| %>
   <%= order_form.hidden_field :plan_id, :value => 2 %>
   <%= order_form.hidden_field :price_id, :value => 2222 %>
  <% end %>

谢谢,Alex

1 个答案:

答案 0 :(得分:2)

尝试将orders_attributes添加到attr_accessible语句中的属性列表中。