rails_developer
rails_developer

Reputation: 149

how is collection_select processed in the controller?

I made a drop down list using collection_select

<%= collection_select(:page, :user_id, @users, :id, :full_name) %>

that part works fine. I am having trouble on saving it/processing it using the controller, the APIdock isn't very helpful on that part and I haven't been able to get the one example I found to work. Can anyone explain to me how I can process the selected value in the controller?

Upvotes: 0

Views: 1241

Answers (2)

vajapravin
vajapravin

Reputation: 1383

To store page values in model which should specify 'has_many :pages' in user.rb.

@user = User.find(params[:user_id])
@user.pages = params[:page]

params[:page] returns an array of values which will be store in current model record.

Upvotes: 0

rewritten
rewritten

Reputation: 16435

You will have a value

params[:page][:user_id]

which will correspond to the value selected in the form. You can see it inspecting the params variable.

IT is a number, the ID of the selected user. You could load the user by

@user = User.find(params[:page][:user_id])

but it's useless. In fact, if the user_id property of the page is accessible, then with the usual

@page.update_attributes(params[:page]) # in the update action

or

@page.create(params[:page]) # in the create action

you will get the user in the page as @page.user.

Upvotes: 1

Related Questions