Submitting parameters with form_tag method to update gives undefined route error

I have a form like this:

= form_tag item_path(@item) do
  # some inputs

      

This gives the following HTML:

<form method="post" action="/items/1" accept-charset="UTF-8">
  <!-- some inputs -->
</form>

      

When I submit it, I get the following error:

No route matches [POST] "/items/1"

      

However, when I use SimpleForm on the same element - the HTML is almost the same and it works:

# code:
= simple_form_for @item do |f|
  ...
# output:
<form id="edit_item_1" novalidate="novalidate" method="post" action="/items/1" accept-charset="UTF-8">
...

      

Do you see errors in my code?

How can I make sure my form_tag is sending parameters to the item's update method?

+3


source to share


2 answers


I'm not sure why simple_form_for works. But usually Rails' form_tag methods generate the form using the POST POST method.

<form method="post"

      

So, in your case, if you want to submit a form to update, then the http method must be PUT. You can explicitly specify the http method in rails form_tag.



form_tag item_path(@item), :method => :put do |f|

      

Usually people always prepare form_for instead of form_tag and they will reuse the form template to create and update. As form_for will set the correct http method based on post status. If the entry is new, it will set the http POST else PUT method.

+3


source


Have you defined the path in your routes.rb file?

resources :items, only: :create

      



or post "/items/:id" => "items#create"

I suggest the first

0


source







All Articles