title: My controller is -persistently- sending wrong params[:id] in Rails?!
body: <p>I'm new to Ruby on Rails &amp; to web programming.
  In my application I have two models; Directorate which <code>has_many :users</code>, and User which <code>belongs_to :directorate</code>.
  When creating a new user, I use <code>&lt;%= f.collection_select(:directorate_id,Directorate.all, :id, :name) %&gt;</code> in the new.html.erb form to assign the new user to specific directorate. However, I want to build a user-friendly interface for the dba that lists all directorates; and listing all users beside each directorate, with a link to assign any user to a specific directorate.</p>
  
  <p>What I did is the following:</p>
  
  <p>In Directorate model, I defined the following function:</p>
  
  <pre><code>   def assign_user!(user)
        user.update_attributes(directorate_id: @directorate)
     end  
  </code></pre>
  
  <p>and in the directorates controller, I defined the following action:</p>
  
  <pre><code> def assign_user
     @directorate = params[:directorate]
     assign_user! params[:user]
     redirect_to directorates_url
   end
  </code></pre>
  
  <p>Now, directorates/index.html.erb contains the following:</p>
  
  <pre><code>    &lt;h1&gt;Listing directorates&lt;/h1&gt;
  
  &lt;table&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;Info&lt;/th&gt;
    &lt;/tr&gt;
  
  &lt;% @directorates.each do |directorate| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= directorate.name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= directorate.info %&gt;&lt;/td&gt;
  
      &lt;td&gt;&lt;%= link_to 'Show', directorate %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Edit', edit_directorate_path(directorate) %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Destroy', directorate, confirm: 'Are you sure?', method: :delete %&gt;&lt;/td&gt;
      &lt;%= @directorate = directorate%&gt;
  &lt;%= render 'users_form' %&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  &lt;/table&gt;
  
  &lt;br /&gt;
  
  &lt;%= link_to 'New Directorate', new_directorate_path %&gt;
  </code></pre>
  
  <p>and, -users_form.html.erb contains the following form (which is supposed to list all users beside each directorate, with a link to assign any user to a certain directorate):</p>
  
  <pre><code>    &lt;h1&gt;Listing Users&lt;/h1&gt;
  
      &lt;table&gt;
        &lt;tr&gt;
          &lt;th&gt;User Name&lt;/th&gt;
        &lt;/tr&gt;
  
        &lt;% @users.each do |user| %&gt;
        &lt;tr&gt;
  
          &lt;td&gt;&lt;%= user.username %&gt;&lt;/td&gt;
  
          &lt;td&gt;&lt;%= link_to 'Assign to Current Directorate', {controller: 'directorates', action: 'assign_user', directorate: @directorate, user: user}, :method =&gt; :put %&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;% end %&gt;
      &lt;/table&gt;
  
  &lt;br /&gt;
  </code></pre>
  
  <p>Here is the problem, when listing directorates &amp; click on the 'Assign to Current Directorate' I receive the following error:</p>
  
  <pre><code>http://127.0.0.1:3000/directorates/assign_user?directorate=4&amp;user=5
  
  ActiveRecord::RecordNotFound in DirectoratesController#update
  
  Couldn't find Directorate with id=assign_user
  Rails.root: /home/ehab/sites/IAMS
  
  Application Trace | Framework Trace | Full Trace
  app/controllers/directorates_controller.rb:61:in `update'
  Request
  
  Parameters:
  
  {"_method"=&gt;"put",
   "authenticity_token"=&gt;"L5tz3hv2IW0meE79qUq0/tjfGKwDlpC23hOeAWtmTvk=",
   "directorate"=&gt;"4",
   "user"=&gt;"5",
   "id"=&gt;"assign_user"}
  </code></pre>
  
  <p>It's clear that the params is submitting "id"=>"assign_user" which I don't want, what i want is "id"=>"directorate.id" (4 in the above example). What shall I do to fix this issue?!  </p>