jeudi 15 septembre 2016

Cant mass assign error even after specifying the attributes in attr_accessible

I have a User model and Profile model as following

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

profile.rb:

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

My form page:

<%= 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 %>

I get the following error

 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'

I have specified the attributes in the attr_accesible but I dont understand why I still get this error. Please help.

Aucun commentaire:

Enregistrer un commentaire