Hi there,
I used restful authentication to generate the authenticated system, but i still want to add a profile model as a part of user model.
So between user & profile I use has_one association to connect them.
Create - OK But when i want to edit someone's profile, the data not update in sql.
my user model is like this:
has_one :profile attr_accessible :login, :email, :password, :password_confirmation
def profile_attributes=(profile_attributes) profile_attributes do |attributes| profile = profile.detect {|p| p.id == attributes[:id].to_i} profile.attributes = attributes end end
My Profile model is like this: belongs_to :user
The migration has the user_id foreign_key set up.
In the view/user/edit.html.erb, i wrote enclosed codes:
<% form_for :user, :url => user_url(@user), :html => { :method => :put } do |f| %> <p>Email:<br /><%= f.text_field :email, :size => 60 %></p> <% fields_for "user[profile_attributes]", @user.profile do | p_form| %> <p> Name: <%= p_form.text_field :name, :index => nil %> </p> <p> Gender: <%= p_form.text_field :gender, :index => nil %> </p> <p> Birthday: <%= p_form.text_field :birth_on, :index => nil %> </p> <p> Place: <%= p_form.text_field :place, :index => nil %> </p> <p> About Me: <%= p_form.text_field :about_me, :index => nil %> </p> <%= p_form.hidden_field :id, :index => nil %> <% end %>
<%= submit_tag 'Update' %> <% end %>
When I click update button, error says: Couldn't find Profile without an ID
Or Can't mass-assign in a protected attributes in the development log.
Can anyone help me?