So I made this custom Route
on my routes.rb
get'dashboard_report_m/:date/:branch_id'=>'reports#monthly_and_branch'
I'm Getting Routing Error, No route matches [GET] "/dashboard_report_m"
on my rake routes I have this
on rake routes
GET /dashboard(.:format) reports#today_admin
GET /dashboard_report/:date/:branch_id(.:format) reports#date_and_branch
GET /dashboard_report_m/:date/:branch_id(.:format) reports#monthly_and_branch
all the other routes I made are fine but this one just doesn't seem to work.
I tried removing the keys ':date/:branch_id'
and it would work just fine.
I have already made similar routes and they all work just fine except for this one.
Some things to check:
Is there a ReportsController with a ​monthly_and_branch action?
Does the error occur if you visit /dashboard_report_m/2016-09-20/1234 directly or are you using a path helper?
UPDATE
OK so you are accessing the path http://localhost:3000/dashboard_report_m/?date=2016-09&branch_id=1 - you are passing the parameters in as query params, this is not how your route is set up. The way you have it now it is expecting dashboard_report_m/2016-09/1. You need to either remove the date and branch_id params from your route or change the way you access the URL. I suggest reading the Rails Routing from the Outside In guide.
When you access the following route:
localhost:3000/dashboard_report_m/?date=2016-09&branch_id=1
This is a GET request to 'dashboard_report_m', with query parameters: params['date'] = '2016-09' and params['branch_id'] = '1'.
What you should instead be doing is accessing this route:
localhost:3000/dashboard_report_m/2016-09/1
This is a GET request to 'dashboard_report_m/:date/:branch_id' - i.e. using the bound parameters of date and branch_id.
Further reading: Understand the difference between bound parameters and the query string. This is by no means specific to Rails; it's at the core of how all web applications work.
I've met strange error. Im not sure this is bug. However i never met this strange behavior before.
resource :watches
Makes such strange routing table:
watches POST /watches(.:format) watches#create
new_watches GET /watches/new(.:format) watches#new
edit_watches GET /watches/edit(.:format) watches#edit
GET /watches(.:format) watches#show
PUT /watches(.:format) watches#update
DELETE /watches(.:format) watches#destroy
As you see no ID param and messed actions
On same time:
resources :mibs
Make proper routes
mibs GET /mibs(.:format) mibs#index
POST /mibs(.:format) mibs#create
new_mib GET /mibs/new(.:format) mibs#new
edit_mib GET /mibs/:id/edit(.:format) mibs#edit
mib GET /mibs/:id(.:format) mibs#show
PUT /mibs/:id(.:format) mibs#update
DELETE /mibs/:id(.:format) mibs#destroy
I thought that is could be somehow inflector problem, but trying using "rockets" instead of "watches" give same result:
rockets POST /rockets(.:format) rockets#create
new_rockets GET /rockets/new(.:format) rockets#new
edit_rockets GET /rockets/edit(.:format) rockets#edit
GET /rockets(.:format) rockets#show
PUT /rockets(.:format) rockets#update
DELETE /rockets(.:format) rockets#destroy
Anything except my first two resources (servers and mibs) make such result.
Probably corrupted routing cache somewhere?
resource indicates a singleton resource: in other words, you're telling Rails that there's only ever one watch for each user, so passing IDs would be useless.
resources is the standard invocation for getting routes with IDs attached.
So, essentially, the problem is an inflector one, but for resource or resources, not for the name of your routes. For more information, check out the Ruby on Rails routing guide. It does a good job explaining the difference between singleton resources and the more usual kind.
I am learning Ruby on Rails and i did this scaffolding command:
rails g scaffold Alex
and it ran and created all the resources for me. So I tried to make a link from my index page to the alex page like this:
<%= link_to "Alex Link", alex_path(#alex) %>
(I am still not sure what that #alex part is, but it was in the other examples so I tried to have that there)
and in my routes.rb this code was created:
resources :alexes
get "home/index"
and when I tried to load the link, it gave me this error:
No route matches {:action=>"show", :controller=>"alexes"}
Just in case, here is the output from rake routes:
alexes GET /alexes(.:format) alexes#index
POST /alexes(.:format) alexes#create
new_alex GET /alexes/new(.:format) alexes#new
edit_alex GET /alexes/:id/edit(.:format) alexes#edit
alex GET /alexes/:id(.:format) alexes#show
PUT /alexes/:id(.:format) alexes#update
DELETE /alexes/:id(.:format) alexes#destroy
home_index GET /home/index(.:format) home#index
root / home#index
test POST /test(.:format) tests#create
new_test GET /test/new(.:format) tests#new
edit_test GET /test/edit(.:format) tests#edit
GET /test(.:format) tests#show
PUT /test(.:format) tests#update
DELETE /test(.:format) tests#destroy
What is wrong with the way that I made the link and how can I make it hit the controller before it goes to the view?
Thanks!
alex_path is for showing a specific Alex object. To use it, #alex needs to be an instance of an Alex object loaded by your controller.
You say you want to link to the "Alex page", which makes me think you want the listing of all Alex objects, or the index action of your AlexController. If this is the case, you should use alexes_path instead of alex_path(#alex).
If you actually do want to link to a single specific Alex, you'll need to load an instance from the database:
def my_action
# make a specific Alex object available to the view
#alex = Alex.find(...)
end
As an aside, you also ask:
how can I make it hit the controller before it goes to the view?
Your controller will always be hit before the views are rendered. It is impossible for a view to be rendered without an action being invoked.
I am new in rails,and I create the first rails application (Blog) follow the guide at rails's docs step by step.
However when I run the application,I found something I can not understand.
http://localhost:3000/posts/2
With GET method,this will return the details of post whose id is 2.
But when update this post,I found the action of the form is '/posts/2'.
When delete the post,I found rails create a form element in the body with action '/posts/2' and method POST,so I wonder how does rails know update or delete this post?
Since I do not found any condition word in the post controller.
Anyone can tell me?
Which action is run is determined by a unique combination of the path and the verb
Do the command rake routes in your app folder, and you'll see a list of routes.
Paths, are the RESTful routes to your resources.
Verbs are GET POST DELETE, and PUT
I would also recommend the Rails Routing Guide as reading, which explains this in a lot more detail.
Here's sample output from rake routes from one of my apps:
path name|verb |path |action
============================================================================
alias_lists GET /alias_lists(.:format) alias_lists#index
POST /alias_lists(.:format) alias_lists#create
new_alias_list GET /alias_lists/new(.:format) alias_lists#new
edit_alias_list GET /alias_lists/:id/edit(.:format) alias_lists#edit
alias_list GET /alias_lists/:id(.:format) alias_lists#show
PUT /alias_lists/:id(.:format) alias_lists#update
DELETE /alias_lists/:id(.:format) alias_lists#destroy
Note that show, update and destroy all have the same path, but a different verb.
show's verb = GET
update's verb = PUT
destroy's verb = DELETE
I am having a hard time understanding routes in rails 3. I created two scaffolds: Users and magazines. The users are able to login, but I am unable to link to the magazine page. I know it has to do with creating a route. If I navigate via the URL to localhost:3000/magazines, I can see the multiple magazines I created and each user associated with each magazine. I just can't seem to connect the dots. I want to create a link from the user page to the magazine page. I know this is basic, but all the routes documentation just are not making sense to me. Thanks so much for your time.
Resource pointed out in previous answers is awesome and that is where I got started. I still refer that in case I am stuck somewhere. One thing I find missing in the recourse is that it doesn't include the explanation of reading the routes table i.e. output of command rake routes and it takes time to fit the pieces together. Although if you read through the whole guide patiently, you can fit the pieces together.
On my system 'rake routes' gives the following output (excerpt relevant to resources :messages)
messages GET /messages(.:format) {:action=>"index", :controller=>"messages"}
POST /messages(.:format) {:action=>"create", :controller=>"messages"}
new_message GET /messages/new(.:format) {:action=>"new", :controller=>"messages"}
edit_message GET /messages/:id/edit(.:format) {:action=>"edit", :controller=>"messages"}
message GET /messages/:id(.:format) {:action=>"show", :controller=>"messages"}
PUT /messages/:id(.:format) {:action=>"update", :controller=>"messages"}
DELETE /messages/:id(.:format) {:action=>"destroy", :controller=>"messages"}
All the columns in this table give very important information:
Route Name(1st Column): This gives the name of the route, to which you can append "_url" or "_path" to derive the helper name for the route. For example, first one is the "messages", so you can use messages_path and messages_url in your views and controllers as a helper method. Looking at the table you can tell messages_path will generate a path of form "/messages(.:format)". Similarly, other route names generated are "new_message", "edit_message" and "message". You can also control the naming of routes.
HTTP Verb(2nd Column): This gives the information about the http verb which this route will respond to. If it is not present, then it means this route will respond to all http verbs. Generally browsers only support, "GET" and "POST" verbs. Rails simulate "PUT" and "DELETE" by passing a parameter "_method" with verb name as value to simulate "PUT" and "DELETE". Links by default result in a "GET" verb and form submissions in "POST". In conjunction with the first column, if you use messages_path with http "GET" it would match first route and if you use it with "POST" it will match second route. This is very important to note, same url with different http verbs can map to different routes.
URL Pattern(3rd Column): Its like a limited featured regular expression with syntax of its own. ":id" behaves like (.+) and captures the match in parameter "id", so that you can do something like params[:id] and get the captured string. Braces () represent that this parameter is optional. You can also pass these parameters in helpers to generate the corresponding route. For example if you used message_path(:id => 123) is will generate the output "/messages/123".
Where this routes(4th Column): This column generally tells the controller and the corresponding action which will handle requests matching this route. There can be additional information here like constraints if you defined any.
So if "localhost:3000/magazines" is the page you want, you should check the routes table with url pattern as "/magazines(.:format)" and disect it yourself to find out what you need. I would recommend you read the whole guide from top to bottom if you are just starting rails.
(This might be just an overkill to write all this here, but I faced many problems due to this info not being available in a consolidated manner. Always wanted to write it out and finally did. I wish it was available on http://edgeguides.rubyonrails.org/routing.html in a separate section.)
This is a really nice summary of the routes: Rails Routing from the Outside In.
What about:
<%= link_to "magazines", magazines_path %>
You should be aware of all routes created by the mere scaffold. It's quite easy and explained in Rails guides.
Here are the details: http://guides.rubyonrails.org/routing.html#paths-and-urls
You also might want to check these RailsCasts:
http://railscasts.com/episodes/203-routing-in-rails-3 ,
http://railscasts.com/episodes/231-routing-walkthrough ,
http://railscasts.com/episodes/232-routing-walkthrough-part-2
and these pages:
http://edgeguides.rubyonrails.org/routing.html
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
http://markconnell.co.uk/posts/2010/02/rails-3-routing-examples
A few things, in addition to what others have already said:
magazines_path is the most likely name of the link to the index page.
<%= link_to "Magazines", magazines_path %>
so that should do the trick. But if you want to see routes, I'd recommend you just run rake routes, which will list whatever Rails is considering a valid route name. If you want to see how they're used, check out the view pages for your scaffold. app/views/magazines/show.html.erb, for example, might have something like this at the bottom:
<%= link_to 'Edit', edit_magazine_path(#magazine) %> |
<%= link_to 'Back', magazines_path %>
The edit link goes to the edit page (/magazines/[ID]/edit) for the magazine stored in #magazine and the back link goes to the index page (/magazines/). The show page for an individual magazine would be magazine_path(#magazine) and the new path would be new_magazine_path(#magazine).
You should definitely check out the resources others have posted -- Rails routing is flexible but very "magical" -- but in any case, that should help give you some context.
Also, this should be automatically generated, but I think most people are assuming your config/routes.rb contains something like the following:
My::Application.routes.draw do
resources :magazines
resources :users
# or the above combined as resources :magazines, :users
end
This is what tells rails to build out the basic routes for index, new, edit, show, create, update, destroy for a particular resource.
By far, the best explanation of routes I've found is in The Rails 3 Way by Obie Fernandez (founder of Hash Rocket).