0

So I have devise setup to perform registration. After registration the user is redirected to profiles#new however I can't attach profile to current_user id

Actually it doesn't work at all. Here is what I have in my profiles_controller.rb

# POST /profiles

def create @profile = current_user.Profile.new(params[:profile])

respond_to do |format|
  if @profile.save
    format.html { redirect_to(@profile, :notice => 'Profile was successfully created.') }
  else
    format.html { render :action => "new" }
  end
end

end

leading to undefined method `Profile' for #

2 Answers 2

0

So the User model has_one :profile ?

If so, you likely want:

@profile = current_user.profile.build(params[:profile])

Note that case ('profile' vs 'Profile') is important here.

2
  • Thanks. Unfortunately here is what happens undefined method `build' for nil:NilClass. It works when I do it in 2 steps. Any reason why? #@user = current_user #@profile = @user.create_profile(params[:profile]
    – Olivier
    Jan 15, 2011 at 19:54
  • Are you sure you have a has_one :profile (or belongs_to :profile ... depending on which has the foreign key) on the User model? Jan 15, 2011 at 20:06
0

I think that you should use

@profile = current_user.build_profile(params[:profile])

Check rails api here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.