nested resources and scoped ActiveRecord calls

Ok, I've got a nested resource, set up like so:

map.resources :users, :has_one => :profile

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."

Anyone know what's going on?

Have you tried simply using:

form_for @profile do |f|

If there is only one route to the profile (the nested one) I believe
it will correctly build the path for you.

AndyV wrote:

Have you tried simply using:

form_for @profile do |f|

No such luck. It tries to route to "profile_path", which doesn't exist.

Then it sounds like you want the more explicit version:

form_for :profile, :url=>user_profile_path(@user, @profile), ... do |
f>
...
end

AndyV wrote:

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.

Sorry, I think I've been after the wrong end of the equation.

With a has_one relationship you should be using
@user.build_profile(...)