No Route Matches Rails 4 - ruby-on-rails

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 %>

Related

Http request work differently from the parameter I gave in Ruby on Rails

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.

how to put controller/action in link_to rails 4.1

I have:
<%= button_to '+',{:controller=>"line_items",:action=>'create',:menu_id=> line_item.menu_item,:remote=>true}%>
I have put same code for link:
<%= link_to '+',{:controller=>"line_items",:action=>'create',:menu_id=> line_item.menu_item,:remote=>true}%>
But link_to is redirect me to the line_item_index page.I want that link_to will work like this button.please help me i am new in rails.
link_to uses http GET for the resource you're linking while button_to uses http POST to your controller action.
adding :method => :post explicitly to your link_to tag makes it behave as http POST event
method link_to creates link and method button_to creates form. They are not the same.
From button_to you are using the form with method POST and it works ok, but with link_to you are using method GET, and this is a problem.
To solve the problem, try this:
link_to '+',{:controller=>"line_items",:action=>'create', :menu_id=> line_item.menu_item, :remote=>true, method: :post}
more explanation at:
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Rails button_to and using patch vs post

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.

Rails app submitting 'post' request to a custom 'get' route

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 %>

Rails button_to fails with path doesn't exist for a path that exists

Writing my first, very simple Rails application, a simple admin app to track work for one of our departments. The generated index page for people has a link_to on it to add a new person. I tried to change that to button_to and it fails saying the path /people/new doesn't exist, though obviously it does since link_to goes to the same place.
I'm using Rails 3/Ruby 1.9.2. I have this code on my /app/views/people/index.html.erb page:
<%= link_to 'New Person', new_person_path %>
<%= button_to "New", :controller => "people", :action => "new" %>
The link_to works. The button_to fails with this:
Routing Error
No route matches "/people/new"
Also tried just
<%= button_to 'New Person', new_person_path %>
Same error. Odd.
button_to defaults to the post method. Try putting :method => :get in there. This is why link_to works.
There's a good explanation for this, as always :)
link_to uses GET as default, where button_to uses POST. And there's no POST route that matches, only a GET route.
If you want to use button_to, you can add :method => :get to your buttons params and it will use GET.
Did you set up your routing options in config/routes.rb? Check if you have this in your routes.rb file:
resources :people
Check this guide for more informations about how routes work.
Is your button_to inside a form? button_to creates a form of its own so this would create a form within a form and likely break routing.

Resources