When I create a user, I'm using this route to go the new profile page:
new_user_profile_path(@user)
I'm my Profiles controller, I'm using a before_filter to determine the
user:
before_filter :load_user
def load_user
@user = User.find(params[:user_id])
end
And in my "new" action:
def new
@profile = @user.profile.new
end
In my view, I have:
form_for([@user, @profile]) do |f|
But I get an error telling me that @user.profile is nil.
The :user_id paramter is coming through fine, and @user has all the
proper data in it. So is the problem with the scoped ActiveRecord call
(@user.profile.new)?
If I use @profile = Profile.new, I get an error telling me
"user_profiles_path" in invalid (which I guess is being generated by the
form_for). This is correct, since User should only have one Profile, not
the plural "profiles."
Then it sounds like you want the more explicit version:
form_for :profile, :url=>user_profile_path(@user, @profile), ... do |
f>
...
Still no luck. Looking at my log, I can see that @user.profile.new (or
@user.profile = Profile.new) is trying to grab a pre-existing profile
relative to the user from the database.
I cannot figure out how to instantiate a new profile on a user, and keep
my nested routes intact.