I'm having a really hard time getting rails to use the 'button_to' tag with a patch request vs post.
This is my tag:
<%= button_to 'Start Game', { :controller => 'provides', :id => #prov_id}, :method => :patch %>
I'm using 'resources :provides' in my routes, and I have an update method in my Provide controller.
Here is the output for provides when I run rake routes..
provides GET /provides(.:format) provides#index
POST /provides(.:format) provides#create
new_provide GET /provides/new(.:format) provides#new
edit_provide GET /provides/:id/edit(.:format) provides#edit
provide GET /provides/:id(.:format) provides#show
PATCH /provides/:id(.:format) provides#update
PUT /provides/:id(.:format) provides#update
DELETE /provides/:id(.:format) provides#destroy
Everything should be working, but I get the below error...
Started PATCH "/provides?id=1" for 127.0.0.1 at 2014-03-12 19:20:28 -0500
ActionController::RoutingError (No route matches [PATCH] "/provides"):
What is going wrong here? This is driving me insane, any help is highly appreciated.
You have a route: PATCH /provides/:id(.:format), but there is no PATCH route for "/provides?id=1"
Try:
<%= button_to 'Start Game', provide_path(#prov_id), :method => :patch %>
See more: http://guides.rubyonrails.org/routing.html
I am pretty sure you need to have a form tag for a PATCH since browsers don't currently generate non AJAX PATCH requests.
So pass :method => :patch to form_for as follows:
<%= form_for #provides, method: :patch do |f| %>
<%= f.hidden_field :foo, value: 'somevalue' %>
<%= f.submit 'Start Game' %>
<% end %>
This will send a method parameter which rails will detect and your controller can handle as a real PATCH. I included in the example the ability to pass hidden parameters for the actual patch as its unclear what usefulness PATCH would offer in your example otherwise.
Note: Don't try and embed the above html inside another form. The HTML spec doesn't permit nested form tags.
Related
Hi I'm new to rails and MVC but I'm trying really hard to learn. Right now I'm using AASM to make a transition from in_draft to published.
I'm being able to make the change in rails console but when trying to use a link_to I got the error in the question
`#/app/views/welcome/dashboard.html.erb
<% if article.may_publish? %>
<%= link_to 'Publish', '/articles/#{article.id}/publish', method: :put, class: "alert-link" %>
<%end%>
This is mi route
put '/articles/:id/publish', to: 'articles#publish'
And my articles_controller publish method
def publish
#article.publish!
redirect_to #article
end
you are really, really close! You need to use double quotes to be able to infer using #{}.
<%= link_to 'Publish', '/articles/#{article.id}/publish', method: :put, class: "alert-link" %>
should be:
<%= link_to 'Publish', "/articles/#{article.id}/publish", method: :put, class: "alert-link" %>
Welcome to rails. I would like to suggest you to use member for adding a RESTful put action. Rails routing
resources :articles do
put :publish, on: :member
end
To resolve your current given route problem, Please as: :public_article.
put '/articles/:id/publish', to: 'articles#publish', as: :public_article
Enjoy
I am trying to reach the edit page for my character class, but for some reason it is routing with a POST when it should be a GET. All similar questions have not helped.
Here is my edit function:
def edit
end
Here are my routes:
<%= button_to 'Edit Character', edit_character_path(#character) %>
characters_path GET /characters(.:format) characters#index
POST /characters(.:format) characters#create
new_character_path GET /characters/new(.:format) characters#new
edit_character_path GET /characters/:id/edit(.:format) characters#edit
character_path GET /characters/:id(.:format) characters#show
PATCH /characters/:id(.:format) characters#update
PUT /characters/:id(.:format) characters#update
DELETE /characters/:id(.:format) characters#destroy
here is my edit link:
<%= button_to 'Edit Character', edit_character_path(#character) %>
The problem is that button_to, acccording to documentation, generates a form, and the form method is post
Any reason for which you are ussing button_to ?
Otherwise you can just use a link_to and add a custom class:
<%= link_to 'Edit Character', edit_character_path(#character), class: 'my-custom-class' %>
so you can add the css for your custom class and make it look like a button
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 %>
I have a Friendships model. I need to create a link that posts an update using a link_to. I also need the link_to to add a param with the decision.
How can I build this link_to?
I've tried this:
<%= link_to "Accept Request", friendships_path(:id => #user.id, :decision => "accept"), :method => :put %>
This fails as it posts to:
Started POST "/friendships?decision=accept&id=5379" for 127.0.0.1 at 2011-12-18 17:07:24 -0800
Parameters: {"authenticity_token"=>"Cj/5vmwQWZ4vXStiD1/exxwQJedC9x70gLTrvgUxfpc=", "decision"=>"accept", "id"=>"5379", "uuid"=>"friendships"}
Where it should have posted to /friendships/33?decision=accept with a PUT parameter to trigger the Controller's update method, right?
Thanks