I added the method: patch to the form_with helper (like it said to in this SO post)
<%= form_with(model: [:admin, contest], method: :patch) do |form| %>
...
and the <input type="hidden" name="_method" value="patch"> shows up (like it's supposed to) but then when I click "Submit", the request is still a POST request and I get the error
No route matches [POST] "/admin/contests/7"
I had the same issue, and resolved it by changing it to:
method: "patch"
Update: You're right, "patch" and :patch do the same. I also added the url from my routes file:
model#update
I think I need some more info. What does your controller look like? What model is it? Have you tried specifying the model and the url in form_with?
Related
I have article model and i want to give it votable ability with acts_as_votable gem.I have like_article route for like article and its PUT method and i have that route in my file
<%= link_to like_article_path(article), method: :put do %>
Like
<%= article.get_upvotes.size %>
<% end %>
if i hit the like button i'm getting No route matches [GET] "/articles/11/like" error it act like get method but i gave method: :put parameter it should be put why it dowsnt see my parameter how can i fix that?
I dont know why but i used button_to instead of link_to it worked.
I have a route and form that looks like this:
<%= form_for #address, url: {action: "update_contact", controller: "checkouts"}, html: {class: ""} do |f| %>
My route looks like:
post "checkouts/:cart_token/update_contact" => "checkouts#update_contact", as: "checkouts_update_contact"
For updates the form is looking for a PATCH which I haven't defined, and so I get an error when the #address model already exists i.e. updates
How can I make my form always POST no matter what?
Add method: :post
<%= form_for #address,
url: {action: "update_contact", controller: "checkouts"},
html: {class: ""},
method: :post do |f| %>
Without that Rails adds a hidden field which is used to fake a PATCH request when the form is used to update an object.
<input type="hidden" name="_method" value="patch" />
how to set method inside form to put in form_tag in rails?, i have form_tag like this :
<%= form_tag(url, :method => :put, :multipart => true, :class =>"form-horizontal") do %>
......
<% end %>
but if i inspect element, form not have method "put" but still "post"?
<form accept-charset="UTF-8" action="/admin/stores/1/information/social_update" class="form-horizontal" enctype="multipart/form-data" method="post">
....
</form>
why???
According to the docs:
If “patch”, “put”, “delete”, or another verb is used, a hidden input with name _method is added to simulate the verb over post.
Therefore, the following code will output the following markup:
form_tag('/posts/1', method: :put)
#=> <form action="/posts/1" method="post"> ... <input name="_method" type="hidden" value="put" /> ...
This is basically a backwards compatible way of implementing the PUT protocol across browsers. Although the form's submission method is POST, because of the hidden form input, Rails understands the desired submission method to be PUT.
The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PATCH" and "DELETE" requests (besides "GET" and "POST"). However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.
So, it's perfectly ok to have POST instead of PUT.
Reference
Try entering
:method => 'get'
That should do the trick and display get when you inspect the element.
I generated a scaffold for Studio. Now am linking from Products view
<%= button_to 'Customize', new_studio_path %>
resources :studios is placed in routes.rb
I am still getting No route matches [POST] "/studios/new"
If someone could help me figure this out I would appreciate it tons.
Here is a gists of the files I am working with for this. https://gist.github.com/JRizzle88/7861628
Button's default HTTP method is POST, what you need is method GET, so you need to specify this explicitly:
<%= button_to 'Customize', new_studio_path, method: :get %>
Button is sending a POST request, where your server expects a GET request for given url. You need to specify method on a button:
<%= button_to 'Customize', new_studio_path, method: :get %>
The rails convention would be to use link_to which will send a GET request:
<%= link_to 'Customize', new_studio_path %>
I have this button in my app, which is located in the views/deals/mgmt.htm.erb file:
<%= button_to "Deals", deals_mgmt_path, class: "btn btn-default" %>
Here are my 'deals' routes specified in my routes file:
resources :deals
get "deals/mgmt"
When I run 'rake routes', here's the route as I specified:
deals_mgmt GET /deals/mgmt(.:format) deals#mgmt
When I click the button, I get this routing error:
No route matches [POST] "/deals/mgmt"
Why is my rails trying to submit a post request here? I'm thoroughly confused. Thanks in advance!
The helper button_to creates a form, and forms submits POST requests to the server.
Inspect the page and you will see the form.
You should use a link_to, for a request using GET.
You can use this:
<%= button_to 'deals', deals_management_path, {:method => get} %>
Else
it's better to Go with link_to as by default it send get request to Server.
<%= link_to 'deals', deals_management_path %>