Rails - No route matches POST - ruby-on-rails

I have the following in my routes.rb file:
post 'report/mnps/generate' => 'report#mnps_generate', as: 'report_mnps_generate'
Then, in my reports/mnps.html.erb view I have this:
<%= button_to report_mnps_generate_path %>
However, this button redirects to a post method at reports/mnps. Why is this button redirecting there instead of report/mnps/generate?
EDIT
rake routes returns:
Prefix Verb URI Pattern Controller#Action
root GET / home#index
report_index GET /report(.:format) report#index
report_mnps GET /report/mnps(.:format) report#mnps
report_mnps_generate POST /report/mnps/generate(.:format) report#mnps_generate

The definition of button_to states that the first parameter is its name, which is usually used as label. See here:
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
To get a link to the page, you need to write the button like this
button_to('Clickme!', report_mnps_generate_path)
The reason why it loaded the page you stated is that the button is actually on that very same page and is simply reloading it since no other destination was defined in your button_to call.

Related

Routing error in R

I'm new to Ruby and Ruby on rails and have hit a problem with routing. I have 3 controllers, application controller, a bike controller and a ride controller. My routing table is as follows:
Rails.application.routes.draw do
get 'cycle_tracker/index'
resources :rides
resources :bikes
root 'cycle_tracker#index'
When I run rails routes it brings the following:
Prefix Verb URI Pattern Controller#Action
cycle_tracker_index GET /cycle_tracker/index(.:format) cycle_tracker#index
rides GET /rides(.:format) rides#index
POST /rides(.:format) rides#create
new_ride GET /rides/new(.:format) rides#new
edit_ride GET /rides/:id/edit(.:format) rides#edit
ride GET /rides/:id(.:format) rides#show
PATCH /rides/:id(.:format) rides#update
PUT /rides/:id(.:format) rides#update
DELETE /rides/:id(.:format) rides#destroy
bikes GET /bikes(.:format) bikes#index
POST /bikes(.:format) bikes#create
new_bike GET /bikes/new(.:format) bikes#new
edit_bike GET /bikes/:id/edit(.:format) bikes#edit
bike GET /bikes/:id(.:format) bikes#show
PATCH /bikes/:id(.:format) bikes#update
PUT /bikes/:id(.:format) bikes#update
DELETE /bikes/:id(.:format) bikes#destroy
root GET / cycle_tracker#index
In my main view I have the following (I'm simply trying to create a link from my main page to rides/new.
<%= link_to 'rides', :controller => new_ride_path %>
If I try and access http://127.0.0.1:3000/rides/new then it works as expected. However, if I simply try and access http://127.0.0.1:3000 then I get the following:
showing D:/Dev/CycleTracker/app/views/cycle_tracker/index.html.erb where line #2 raised:
No route matches {:action=>"index", :controller=>"rides/new"}
If I try to use new_ride_url instead of path I get the following:
Showing D:/Dev/CycleTracker/app/views/cycle_tracker/index.html.erb where line #2 raised:
No route matches {:action=>"index", :controller=>"http://127.0.0.1:3000/rides/new"}
I imagine this is probably a fairly straightforward issue, but any help appreciated.
<%= link_to 'rides', new_ride_path %>
Try that - you don't need the :controller => part
if you want to use the controller-action style you need to do this:
<%= link_to "Rides" , controller: "rides", action: "new" %>

ruby on rails- non-existent route

I have a controller file,
abc_controller.rb.
I have defined the show method inside it.
I have a view file,
show.html.haml
inside app/views/abc/
In my routes.rb file, I am giving the following command
resources :abc
I have a button
= link_to 'abc', abc_path, class: 'btn btn-default'
But when I click on the button, its not going to the new page.
I am getting non-existent route error.
Since I am a newbie to rails, I am not able to figure what the problem is.
You are getting a error because there is no such path as abc_path. Run rake routes and you'll see the routes that Rails understands. In you example, resources :abc produces the following routes.
abc_index GET /abc(.:format) abc#index
POST /abc(.:format) abc#create
new_abc GET /abc/new(.:format) abc#new
edit_abc GET /abc/:id/edit(.:format) abc#edit
abc GET /abc/:id(.:format) abc#show
PATCH /abc/:id(.:format) abc#update
PUT /abc/:id(.:format) abc#update
DELETE /abc/:id(.:format) abc#destroy
The first column are the named routes. So in order to get to the index action of abc_controller, the route is named abc_index_path. There is an abc_path but it needs an id params which means that you need to pass something to it. In your case, there's no definitive value to pass to this method so as a trial just use abc_path(1) which will redirect you to /abc/1. This goes to the show action with params[:id] set to 1.
If you do resources (plural) the resulting route for show requires an id: /abc/:id(.:format), so abc_path requires that you pass that :id or a object. If you are dealing with a singular abc (resource :abc), the resulting path doesn't require that :id, so you code should work (this is less common, but hard to tell with your "abc" example.

How did Rails generate this path from our model and controller

Ok I kind of understand this part: CRUD verbs and actions http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
and if I go to the route file of the example I have, I also see a resources :orders in it.
But now in the view of a partial names _carts I am seeing this code:
<%= button_to "Checkout" , new_order_path, method: :get %>
What confuses me is the new_order_path ? Where did that come from? What Rails convention rule is allowing us to right this? Especially where did that "new" come from?
When you use resources :orders in the routes, Rails creates 7 routes for new, create, show, update, destroy, list, and edit. All of them are given names, and new_order_path/new_order_url is related to the new action.
These routes are described at the http://guides.rubyonrails.org/routing.html#paths-and-urls
Those path helpers are automatically generated for resources defined in your routes.rb. You can check what route helpers are available by executing rake routes at the command line. They are shown in the left-most column in the table that prints out.
The general pattern of the paths that are created are like so by default:
new_{singular form of resource}_path - Routes to new on GET
edit_{singular form of resource}_path - Routes to edit on GET
{singular form of resource}_path - Routes to show on GET, destroy on DELETE, update on PUT (Soon to be PATCH in Rails 4)
{plural form of resource}_path - Routes to index on GET and create on POST.
There's also helpers that end in _url instead of _path that provide absolute URLs instead of relative paths. The particular action that is hit in your controller depends on the HTTP verb (GET, PUT, POST, DELETE, etc.) used when visiting those URLs.

What is the right way set up address for form?

I have controller my_controller and in it the action my_action. In my_action view I created a form and manually set this: <%=form_tag('/photos/create') do%>. When I send this form to this action in the photos controller, I'll get the error
No route matches [POST] "/photos/create"
(Obviously the action create exist in the photos controller)
My question is, why I am getting this error, when this action exist and, how can I fix it (what is the right way to set up address in the for manually)?
Because your route is not correct. The create action for photos should be to the /photos route instead with a POST. You can verify this by running rake routes at your command line to get a list of all routes and how they are mapped to your controller actions.
Try this instead:
<%=form_tag(photos_path, :method => :post) do%>
For more information: http://guides.rubyonrails.org/routing.html

Rails routes generate Post request for New action in nested resources

I have the following nested resources:
resources :listings do
resources :offers do
member do
put "accept"
put "reject"
end
end
end
In my listings/show.html.haml, I have
= button_to "Make Offer", new_listing_offer_path(#listing)
Now, when I click button, rails generate a POST request, and thus an error:
Started POST "/listings/2/offers/new" for 127.0.0.1
ActionController::RoutingError (No route matches "/listings/2/offers/new"):
If I refresh (GET request), then the page displays correctly.
I believe this incorrect routing only happens when I added two extra actions: accept and reject, which happens to be POST actions.
Is it a bug in Rails, or it is my fault? How should I prevent this error?
Thank you.
The button_to helper creates a form for you which by default will send a POST request to the URL you've specified ("/listings/2/offers/new").
The routing you've specified will not generate a route to handle a POST request to /new. You can inspect your generated routes and the verbs to which they will respond by running the "rake routes" task.
If you are looking to merely link to the form, change your "button_to" to a "link_to" and add CSS for aesthetics.
= link_to "Make Offer", new_listing_offer_path(#listing)
(this GET would route to your OfferController's new action)
If you are looking to actually POST data, you will likely need to change your usage to:
= button_to "Make Offer", listing_offers_path(#listing)
(this POST would route to your OfferController's create action.)

Resources