I am under Rails 3.0.9.
I have the route:
get 'account/index'
There are such information at console:
account_index GET /account/index(.:format) {:controller=>"account", :action=>"index"}
But when I try http://127.0.0.1:3000/account/,
I get No route matches "/account"
Thanks in advance.
Pls refer to Routes explanation for a more detailed explanation. Also, the following line (match :to =>) should be at the top of your routes.rb file before match ':controller(/:action(/:id))(.:format)'. Hope this helps.
match '/account', :to => 'account#index'
Yes, because the route is /account/index and not /account
Try get 'account#index'.
#Lesha, I am relatively new to Rails as well. The following would be a much more generic way in your routes file instead of a get 'controller#action'.
match ':controller(/:action(/:id))(.:format)'
After this is done, you would have to access your page using http://127.0.0.1:3000/account/index
Related
I'm currently defining routes for my pages in the following manner:
get "home/index"
get "photo/index"
get "project/index"
get "home/about"
root :to => 'home#index'
However, I can only seem to be able to create a link to the photo/project index pages by using:
link
link
In the URL, the "index" part also shows up. I can't simple use /photo in the a link, because rails throws a routing error:
No route matches [GET] "/photo"
How would I create a route match for this?
match "/photo", to: "controller#action"
http://guides.rubyonrails.org/routing.html#connecting-urls-to-code
The best answer is to use match. Don't forget to include via to preserve your restriction on the http method:
match "/photo", to: "controller#action", :via => 'get'
I wanted to know what the easiest way to rename a restful route is. Basically I have a controller called Employees and rather than have employees/new I want employees/hire to be used and achieve the same thing and make employees/new an invalid url.
For your specific need, the guide has exactly this example for new, edit, this should work:
resources :employees, :path_names => { :new => 'hire' }
http://guides.rubyonrails.org/routing.html#overriding-the-new-and-edit-segments
One of the best sources of data on routes is the rails guide:
Rails Guide on Routes, also the command
rake routes
This command will show you all the current routes.
But in answer to this specification question
if you look into your routes file you can create new routes
manually.
match 'employee/hire' => 'Employees#new', :via => :get, :as => 'employee_path'
the first argument matches what the browser is looking for.
The second argument is the controller and method.
The third is if it is a get, put, post, or delete call.
The fourth is the name for the path so you can access with the standard name_path type of call from code.
This makes sense?
I am new to ruby on rails.
I used the command 'rails generate controller Courses new'
Then, I edited routes.rb file with:
resources :courses
match '/courses', :to => 'courses#new'
When I access http://0.0.0.0:3000/courses. I get an error:
Unknown action
The action 'index' could not be found for CoursesController.
I think i am missing something. Please help
Thanks.
The line
resources :courses generates the routes for courses like so:
/courses -> coursescontroller#index
/courses/:id -> coursescontroller#show
...
and so on. This is known as 'restful routes'.
If you do not want to direct a url of form 'courses.html' to the 'index' action of your courses controller, but to the 'new' action of your courses controller (which would be highly unusual, by the way), just remove the first line from your routes.rb.
If you want to see what routes you have defined, just do
rake routes
from your rails app directory.
You could use this instead:
resources :courses, :except => :index
match '/courses', :to => 'courses#new'
The except option takes a symbol or an array of actions in the controller you do not want to define resource routes for. In this case, we turn off the route for the index action, /courses/.
Next, we define the same route that would have been defined for the index action, but point it at CoursesController#new.
Put your "match" line before your "resources" line.
I fixed RoutingError in rails 3 using this link. I wanted to redirect users to root page so I added:
match '*a', :to => 'homes#index'
to my routes.rb.
Question is: can I define flash[:error] message in this 'match' line to be displayed on target page?
Regards,
Mateusz
This is similar to Redirect and raise flash message when catch-all in routes
But I did run into this problem and it was giving me an issue because I was using MATCH and when I used GET, the alert wouldn't flash. Eventually I found a working solution using the thread above and applying GET in another manner.
match '*path' => redirect{ |p, req| req.flash[:alert] = "The page you requested is not valid."; '/' }, via: [:get]
This is what I ultimately came up with, via: [:get] being key to making everything work.
And remember to place such code at the end of your routes.rb
Say we have a simple resource called news:
resources :news
The generated paths are in this form /news/:id. I would like to provide a shortcut for this by dropping the /news/, so that /1/ goes to news#show with id 1 and the same for all the other resourceful actions of news.
I figured it's probably something along the lines of
match '/:id(/:action)', :controller => 'news'
but this isn't working.
To change the path to a resource use :path =>
resources :news, :path => "/"
Try this at the very bottom of your routes file:
match ':id', :to => "news#show"
Placing a custom route at the bottom of your routes.rb should work, that will give it lowest priority and allow valid routes to work first:
match '/:id', :to => 'news#show'
It's important to note that this will basically route anything that wasn't previously caught, and does not exist as an actual static file, to that controller/action. You will want to make sure you render your 404 error page if the news record does not exist.