views:

604

answers:

3

I have in a users_controller:

@user = User.new(params[:user])

Passing parameters to a User model which works fine.

I need to pass some additional parameters labeled "profile" to the user model through the users controller.

This didn't work:

    @user = User.new(params[:user, :profile])

Neither did this:

    @user = User.new(params[:user], params[:profile])

I tried a variety of other similar things to get the profile parameters from the view into this model.

I'm using fields_for which works fine to get these fields into the users controller:

  <% fields_for "profile" do |fa| %>

I don't need/want a nested relationship. I just need to simply pass some additional parameters specified in the view to the model.

Thanks!

A: 

Hi,

Try:

@user = User.new(params[:user])
@user.profile = Profile.new(params[:profile])

or

@user = User.new(params[:user])
@user.attributes = params[:profile]
Jonathan
If I use: @user.attributes = params[:profile]How do I access those items in the user model? user.profile_name attributes.profile_nameetc don't work.Thanks!
bandhunt
Hey man, how many attributes do you have on profile (I am a little unclear on what your object model is). If it is not that many, you probably should just simply add the profile_name attributes directly on the user object, and just use the form_for (rather than fields for). If you give us more detail on your object model we can help you better.
Jonathan
hey! profile is a separate model with a larger set of data. In addition the user model will be tied to other types of logins, so the user model needs to be kept clean without anything additional besides login credential stuff.
bandhunt
+1  A: 

fields_for is generally used for assigning params to a related model, so it makes some particular assumptions about the structure of the data.

If you have a profile attribute in your model, you can make the profile parameter part of the params[:user] collection and this will assign the attribute correctly.

You do this by naming the fields:

text_field_tag(user[profile], "val") 

text_field_tag(user[profile][name], "val") 
Toby Hede
I don't have a profile parameter as part of the user model. The profile parameter is outside of the user model. I just need the data from this parameter to be passed to the user model.
bandhunt
In that case just pass it to the user, either using some sort of explicit set (user.profile = params[:profile]), or override the models initialize method and use new.
Toby Hede
In either case, fields_for won't work.
Toby Hede
Thanks for the help! I saw you play drums. I'm currently writing a brand new version of iSound.com if you want to check it out.
bandhunt
A: 

Thanks Toby!

Just to add to his answer. I had to put

attr_accessor :profile

in the user model to access that element.

So the whole thing looks like:

View:

<%= text_field_tag :profile, params[:profile] %></p>

Controller:

@user.profile = params[:profile]

Model:

  attr_accessor :profile

After that I could access the profile element the same as the regular elements in the user model.

bandhunt