0

I am using rails 3.1 and ruby 1.9.3 for my application. And the issue is, I am getting "Can't mass-assign protected attributes" while saving the details.

I have User model as:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

has_one :profile
accepts_nested_attributes_for :profile

attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes, :first_name, :last_name

end

And Profile model as:

class Profile < ActiveRecord::Base

belongs_to :user

end

I am having few fields in "User" model and personal data like first_name, last_name and etc in "Profile" model. And I am trying to get all the required data by customizing the devise signup form as follows:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email %></div>

<div><%= f.label :password %><br />
<%= f.password_field :password %></div>

<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>


<%= f.fields_for :profile do |builder| %>
    <div><%= builder.label :first_name, "First Name" %><br />
    <%= builder.text_field :first_name %></div>
    <div><%= builder.label :last_name, "Last Name" %><br />
    <%= builder.text_field :last_name %></div>
<% end %>


<div><%= f.submit "Sign up" %></div>

Can anyone please tell me where I am going wrong.

  • What are the Profile attributes? Shouldn't you create a profile and then assign to User inside an transaction? From here, I think you probably mislead yourself putting profile's attributes in user's attr_acessible! – waldyr.ar Aug 27 '12 at 14:18

1 Answers1

0

If the first_name and last_name fields are of the profile model then you should mention it as attribute_accessible in the profile model itself.

your profile model should look like

class Profile < ActiveRecord::Base
belongs_to :user
attribute_accessible :first_name, :last_name
end
ashley
  • 2,749
  • 5
  • 26
  • 38
Rajan
  • 1
  • 1