Add a profile model with User model - Edit Problem

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

Email:
<%= f.text_field :email, :size => 60 %>

<% fields_for "user[profile_attributes][]", @user.profile do | p_form| %>

Name: <%= p_form.text_field :name, :index => nil %>

Gender: <%= p_form.text_field :gender, :index => nil %>

Birthday: <%= p_form.text_field :birth_on, :index => nil %>

Place: <%= p_form.text_field :place, :index => nil %>

About Me: <%= p_form.text_field :about_me, :index => nil %>

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

Can anyone help me?

On 7/2/08, *Barrier [email protected] wrote:

Can anyone help me?

You need to iterate through the hash:

profile_attributes do |attributes|

Change to:

profile_attributes.each do |attributes|

Also, be sure to save the profile model.


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com