Note that there are some explanatory texts on larger screens.

plurals
  1. POrails link_to method = post with param
    primarykey
    data
    text
    <p>I have a form that shows a list of animals and lets me select multiple animals and do something with them. Within this same form I need to provide a link for each animal in the listing that will post to another controller and action with the id of that animal posted. Therefore I need a link within my other form (no nesting of forms) that will post to the other URL. This is the route I need to post to:</p> <pre><code>animal_medicalrecords POST /medicalrecords/animal(.:format) medicalrecords#animal </code></pre> <p>This is the form in the animals controller and the link_to I have right now that is not working:</p> <pre><code>&lt;%= form_tag(action: "takeaction") do %&gt; ...some irrelevant form stuff ... &lt;% @animals.each do |animal| %&gt; &lt;%= link_to "Add", animal_medicalrecords_path(:animal_id =&gt; animal.id), :method=&gt; :post %&gt; </code></pre> <p>When I click the link it takes me here ../medicalrecords/animal?id=1 and have these parameters: {"id"=>"animal"} So, I am getting an error that says: Couldn't find Medicalrecord with id=animal</p> <p>What am I doing wrong that it is posting "animal" as the id vs. the actual id (which in this case is 1)?</p> <p>UPDATE: Apparently no matter how I change the link_to it keeps trying to go to the "show" in the controller for medicalrecords instead of the animals method.</p> <p>UPDATE: If I nest a form within my form it works fine... but not the first record listed:</p> <pre><code>&lt;%= form_tag("/medicalrecords/animal") do %&gt; &lt;%= hidden_field_tag(:animal_id, animal.id) %&gt; &lt;%= submit_tag("Add") %&gt; &lt;% end %&gt; </code></pre> <p>But, I can't allow a form within a form. So, I MUST convert this to a link with method of post.</p> <p>The link_to example I've included posts the right parameter with the right value but it posts it to medicalrecords#show instead of medicalrecords#animal.</p> <p>Anyone know what I need to do to fix this?</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.
    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