title: Attaching params to form
body: <p>In my app, a micropost is simply called an "item". On each user's "show" page, there should be a form for making a new "item" that belongs to that user. </p>
  
  <p>I am getting stuck making such a form. </p>
  
  <p>When the form is submitted, the controller action below fires: </p>
  
  <pre><code>@user = User.find_by(name: params[:name])
  @user.items.create(item_params)
  redirect_to 'root'
  </code></pre>
  
  <p>By the way, the 'item_params' method is</p>
  
  <p><code>params.require(:item).permit(:title, :content, :user_id)</code></p>
  
  <p>I am getting an error about the second line. The error is </p>
  
  <p><code>undefined method 'items' for nil:NilClass</code></p>
  
  <p>I checked in the rails console whether I could make a new item by first selecting a user by name </p>
  
  <p><code>@user = User.find_by(name: "tester1")</code></p>
  
  <p>then create an item for it with the below statement:</p>
  
  <p><code>@user.items.create(title: "new_item_title", content: "new_item_content")</code></p>
  
  <p>and the new item was created as expected. I cannot explain the browser error, though. </p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>The form in question is on a "show_user". Here is the log for the HTTP request for that page:</p>
  
  <pre><code>Started GET "/center/show_user?utf8=%E2%9C%93&amp;name=tester&amp;commit=Search" for 12
  .0.0.1 at 2014-03-19 11:46:51 -0700
  Processing by CenterController#show_user as HTML
    Parameters: {"utf8"=&gt;"√", "name"=&gt;"tester", "commit"=&gt;"Search"}
    ←[1m←[35mUser Load (1.0ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."
  ame" = 'tester' LIMIT 1
    Rendered center/show_user.html.erb within layouts/application (2.0ms)
  Completed 200 OK in 23ms (Views: 18.7ms | ActiveRecord: 1.0ms)
  </code></pre>
  
  <p>The form is hosted on this page. Here is the request that is sent with submission of the form:</p>
  
  <pre><code>Started POST "/center/show_user" for 127.0.0.1 at 2014-03-19 11:47:14 -0700
  Processing by CenterController#create_item_owned as HTML
    Parameters: {"utf8"=&gt;"√", "authenticity_token"=&gt;"P6mb63rhTHx6Q17zhfu4WXErG5Pb0
  8t0d/L1RppKAl0=", "item"=&gt;{"title"=&gt;"uno", "content"=&gt;"uno1"}, "commit"=&gt;"Save I
  tem"}
    ←[1m←[36mUser Load (1.0ms)←[0m  ←[1mSELECT "users".* FROM "users" WHERE "users
  "."name" IS NULL LIMIT 1←[0m
  Completed 500 Internal Server Error in 4ms
  
  NoMethodError (undefined method `items' for nil:NilClass):
    app/controllers/center_controller.rb:18:in `create_item_owned'
  </code></pre>
  
  <p>So no information about the user is being passed along with the parameters when the form is submitted. How how I fix this? </p>
  
  <p>Here are my relevant routes:   </p>
  
  <pre><code>get "center/show_user/" =&gt; 'center#show_user'
  
  post "center/show_user" =&gt; 'center#create_item_owned'
  </code></pre>
  
  <p>The "create_item_owned" action is the one described at the beginning of this question. 
  The "show_user" action is </p>
  
  <p><code>@user = User.find_by(name: params[:name])
   @new_item = Item.new</code></p>
  
  <p>How should I forward along those parameters in such a way that the new object created by the form is automatically associated with the specific user in the database (along with automatic creation of foreign keys)? </p>
  
  <hr>
  
  <p>My form is </p>
  
  <pre><code>&lt;%= form_for :item do |x| %&gt;
      Title:  &lt;%= x.text_field :title  %&gt; &lt;br&gt;    
      Content:    &lt;%= x.text_field :content %&gt; &lt;br&gt;   
      &lt;%= x.submit %&gt;
  &lt;% end %&gt;
  </code></pre>