Rails syntax Passing POST parameters from a form to a controller
I'm new to Rails (and fairly new to programming in general) and I am building a web app for myself as a way to learn.  Right now I am modifying scaffolded forms and such.  

My question is with the "create" method in one of my controllers. There are two entities I am concerned with:  the User table and the Habit table.  I created a dropdown box in the _form partial for the Habit views to allow a person to select a user from a list of all available when creating a habit as below

    <%= collection_select  :user, :id, @users, :id, :first_name %>

The habit controller, of course, has

    def new
      @users = User.all
      ...    
    end

This works fine, and when the form submits it posts two hashes of parameters :habit and :user. Now, when I want to process the form input in the create method, I'm not sure how to use the syntax correctly and assign the user_id to the newly create habit.  What I WANT to do is something like this

    def create
       @habit = Habit.new(params[:habit], params[:user])
    end

This, of course, is improper syntax. 

    def create
       @habit = Habit.new(params[:habit])
    end

assigns the params from the :habit hash correctly, but then the user_id is left unset.

What works is the following, but the code is very lengthy, assigning each value manually.

    def create
        @habit = Habit.new(:user_id => params[:user][:id], 
                       :description => params[:habit][:description], 
                       :habit_method => params[:habit][:habit_method], 
                      :time_reqd => params[:habit][:time_reqd],
                      :will_reqd => params[:habit][:will_reqd],
                      :active => params[:habit][:active])

    end

So my question is, when dealing with a form that posts data in multiple hashes, what is the proper way to pass those parameters into some method in a controller?

Rails syntax Passing POST parameters from a form to a controller
===

I'm new to Rails (and fairly new to programming in general) and I am building a web app for myself as a way to learn.  Right now I am modifying scaffolded forms and such.  

My question is with the "create" method in one of my controllers. There are two entities I am concerned with:  the User table and the Habit table.  I created a dropdown box in the _form partial for the Habit views to allow a person to select a user from a list of all available when creating a habit as below

    <%= collection_select  :user, :id, @users, :id, :first_name %>

The habit controller, of course, has

    def new
      @users = User.all
      ...    
    end

This works fine, and when the form submits it posts two hashes of parameters :habit and :user. Now, when I want to process the form input in the create method, I'm not sure how to use the syntax correctly and assign the user_id to the newly create habit.  What I WANT to do is something like this

    def create
       @habit = Habit.new(params[:habit], params[:user])
    end

This, of course, is improper syntax. 

    def create
       @habit = Habit.new(params[:habit])
    end

assigns the params from the :habit hash correctly, but then the user_id is left unset.

What works is the following, but the code is very lengthy, assigning each value manually.

    def create
        @habit = Habit.new(:user_id => params[:user][:id], 
                       :description => params[:habit][:description], 
                       :habit_method => params[:habit][:habit_method], 
                      :time_reqd => params[:habit][:time_reqd],
                      :will_reqd => params[:habit][:will_reqd],
                      :active => params[:habit][:active])

    end

So my question is, when dealing with a form that posts data in multiple hashes, what is the proper way to pass those parameters into some method in a controller?
