submitting params with form_tag to update method gives undefined route error
I have a form like this:
= form_tag item_path(@item) do # some inputs
This gives following HTML:
<form method="post" action="/items/1" accept-charset="UTF-8"> <!-- some inputs --> </form>
When I submit it - I get a following error:
No route matches [POST] "/items/1"
However when I use SimpleForm for the same item - 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 any mistakes in my code?
How do I make sure that my form_tag submits params to the item update method?
I am not sure why simple_form_for works. But normally Rails form_tag methods generates form with http method POST.
So in your case if you want to submit the form to update method then http method should be PUT. You can explicitly specify the http method in rails form_tag.
form_tag item_path(@item), :method => :put do |f|
Normally people will always prepare form_for instead of form_tag and they will reuse the form template for create and update. Because form_for will set correct http method based on record status.If the record is new it will set the http method to POST else PUT.
Did you define the path in your routes.rb file?
resources :items, only: :create
or post "/items/:id" => "items#create"
I suggest the first one