即使在attr_accessible中指定属性后,也无法分配错误

时间:2016-09-15 12:08:28

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

我有一个用户模型和个人资料模型如下

 class User < ActiveRecord::Base
   has_one :profile
   accepts_nested_attributes_for :profile
   attr_accessible :gender,:mobile_number, :profile_attributes
   #...
 end

profile.rb:

 class Profile < ActiveRecord::Base
   belongs_to :user
   attr_accessible :twitter_url, :fb_url
   #...
 end

我的表单页面:

<%= simple_form_for current_user do |f| %>
  <%= f.input :gender, required: true %>
  <%= f.input :mobile_number, required: true %>
  <%= f.simple_fields_for :profile do |p| %>
    <%= p.input :twitter_url %>
    <%= p.input :fb_url %>
  <% end %>
  <%= f.submit "Save" %>
<% end %>

我收到以下错误

 ActiveModel::MassAssignmentSecurity::Error - Can't mass-assign protected attributes: gender, mobile_number, profile_attributes:
 activemodel (3.2.21) lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
 activemodel (3.2.21) lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
 activemodel (3.2.21) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
 activemodel (3.2.21) lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
 activerecord (3.2.21) lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
 activerecord (3.2.21) lib/active_record/persistence.rb:216:in `block in update_attributes'
 activerecord (3.2.21) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
 activerecord (3.2.21) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'

我已经在attr_accesible中指定了属性,但我不明白为什么我仍然会收到此错误。请帮忙。

0 个答案:

没有答案