Remove "index" from url in Rails - ruby-on-rails-3

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'

Related

Rails 3 Route Get No Route matches

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

Rails -- rename restful route

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?

URL helper for a post route in a resource

I have a resourceful route, with a post route nested within it:
resources :groups, :only => [:index, :show] do
post 'send_audit_reminder', :on => :member
end
If I run rake routes, this route shows up just fine:
send_audit_reminder_group POST /groups/:id/send_audit_reminder(.:format)
{:controller=>"groups", :action=>"send_audit_reminder"}
However, I can't seem to figure out how to refer to the send_audit_reminder URL for a given route. I've tried send_audit_reminder_group_path(#group) and send_audit_reminder_url(#group), which both give me the following error:
No route matches {:controller=>"groups", :action=>"send_audit_reminder"}
As you can see from rake routes, there is indeed a route that matches those parameters, and there is also a matching method on the controller.
How can I find the path or URL for this route? I would like not to hard code it, since our apps are deployed to subdirectories on the same virtual host, so a hard-coded absolute path won't work.
And where would I look for documentation or information on this in the future? Since these path and URL helper methods are generated from my routes, I obviously can't look for documentation, and while rake routes tells me that the route is there, it doesn't appear to be there when I try and get the URL.
It might be that you're missing the placeholders and it can't route because of that. The following should work based on your definition:
send_audit_reminder_group_path(group)
Any time you see identifiers like :id or :group_id in your route, you must supply them unless they are in brackets, which declares them as optional, as is the case here with :format. The arguments need to be supplied in the same order they are declared. For this:
/example/:user_id/groups/:id
The arguments to this route would be user_id and id and both must be supplied. Generally with routes you can either use a literal number or string, or a model that supports to_param as all ActiveRecord::Base-derived ones do.
This all stems from declaring with :member, meaning it is specific to a particular record, and not :collection where that is omitted. The Rails Routing Guide explains more.

rails routes with flash

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

How to do this with routing in Rails?

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.