Note that there are some explanatory texts on larger screens.

plurals
  1. PORails syntax Passing POST parameters from a form to a controller
    primarykey
    data
    text
    <p>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. </p> <p>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</p> <pre><code>&lt;%= collection_select :user, :id, @users, :id, :first_name %&gt; </code></pre> <p>The habit controller, of course, has</p> <pre><code>def new @users = User.all ... end </code></pre> <p>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</p> <pre><code>def create @habit = Habit.new(params[:habit], params[:user]) end </code></pre> <p>This, of course, is improper syntax. </p> <pre><code>def create @habit = Habit.new(params[:habit]) end </code></pre> <p>assigns the params from the :habit hash correctly, but then the user_id is left unset.</p> <p>What works is the following, but the code is very lengthy, assigning each value manually.</p> <pre><code>def create @habit = Habit.new(:user_id =&gt; params[:user][:id], :description =&gt; params[:habit][:description], :habit_method =&gt; params[:habit][:habit_method], :time_reqd =&gt; params[:habit][:time_reqd], :will_reqd =&gt; params[:habit][:will_reqd], :active =&gt; params[:habit][:active]) end </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload