How can i customize restful route urls to friendly urls like stackoverflow ?
i have
stackoverflow.com/questions/424/
and i want to have
stackoverflow.com/questions/424/title-of-the-page
map.resources :questions
map.friendly 'questions/:id/:title', :controller => 'questions', :action => 'show'
These are my final customizations. Any better ideas ?
If there's only one controller you want to do this with, the simplest solution would probably be to add a route with an ignored parameter, like so:
# config/routes.rb
map.connect 'questions/:id/:ignored', :controller => 'questions', :action => 'show'
Make sure you put this before the default routes at the bottom of that file. Or, even better, comment them out if you're using named routes and resources (as suggested in the auto-generated comments).
StackOverflow ignores the string second parameter at request processing time. At URL construction time it adds it for humans and (probably) SEO.
I'd start by looking at the capabilities of something like friendly_id and see if those aren't what you're looking for...
Related
I have a rails 2.3.5 application where an action acb is changed to pqr and modified so that it works only for "get" method.
To achive this I have used resource route with options like
map.resources :controller, :member => {:pqr => :get}
The original view file has acb_controller_path link in many places. If I change the path in view file as pqr_controller_path it works fine.
Is there a way I can refer acb_controller_path to controller/:id/pqr ?
You're better off changing the view paths to point to your new route, and I think I'm misunderstanding your question a little but depending which way you're trying to do it, you can try something like this I guess?
map.acb_controller '/controller/:id/pqr', :controller => "controller", :action => "pqr"
I have the following routing rule:
match ':controller/:action/:id'
However when I use
<%= link_to "Link", :action => "some_action", :id => 10 %>
Instead of redirecting to "some_action/10" it redirects to "some_action?id=10"
How can I fix that?
P.S. I know I should be using the path methods, but is there a way to avoid them?
As Matchu said, it should work. Try making your catch-all route the first one in routes.rb. If it works then, you'll know there's another route being evaluated first.
If this doesn't work, post your complete routes.rb file.
i want to change the default route in RoR to what i want:
consider the following example...
:controller/:action/:id
which will give you the route in the browser as:
http://localhost:3000/controller/action/id
now i want to change it to...
http://localhost:3000/this-is-what-i-want/id
we can get an alias for the controller and the action as well like...
resources :controller, :as => "my-custom-name"
and if you want to have the alias for the action, then
resources :controller, :path_names => { :action => 'my-custome-name-1', :action => 'my-custome-name-2' }
BUT i want to change the controller and the action at once... if u noticed the above http://localhost:3000/this-is-what-i-want/id path in the question...
need help...
thanks in advance...
You need a named route.
In Rails2:
map.a_name 'this-is-what-i-want/:id', :controller => 'controller_name', :action => 'action_name'
In Rails3:
match 'this-is-what-i-want/:id' => 'controller_name#action_name'
You want to be using Rest routes, rather than controller/action
I'm going to use "balls" instead of "this-is-what-i-want"
resources :balls
Then, when you link to a ball, do link_to(ball.name, ball).
This will give you a link of http://localhost:3000/balls/45
This rails rest cheatsheet is a good start.
When should one use named routes vs. using url_for with a {:controller => "somecontroller", :action => "someaction"} hash?
Is one preferred over the other and why? Is one more maintainable or more efficient w.r.t. performance?
It might help to understand what named routes are doing.
Defining a Named route creates a wrapper around url_for providing the options required for the created route. Routing resources creates many named routes.
With that in mind, the overhead of calling a named route as opposed to url_for with the options needed is negligible. So if you're linking to a specific resource, named routes are the way to go. They're easier to read, type and maintain.
However, don't discount url_for. It has many creative uses thanks to the way it handles missing options. It is very useful when it comes to keeping views DRY that are used from multiple nested sources. Ie: when you have a blog_posts controller and posts_controller sharing the same views.
I strongly encourage you to read the url_for documentation. To help figure out where those places it makes sense to use url_for are.
I would prefer named routes as it's shorter and does the same thing.
named route is very neat.
map.with_options :controller => "company", :action => "show", :panel => "name" do |m|
m.company '/company/:action/:id/:panel'
end
Then you can call
company_url :id => 1
If you set up your routes and resources carefully, you shouldn't need any hash routes, only named ones (either built-in via map.resource or custom map.<something> ). Hash routes are useful, if you have to create links based on dynamic content. Something like:
link_to #post.title, :controller => (#user.is_admin ? 'admin/posts' : 'public/posts'), :action => 'show', :id => #post
(This is just a forced example, but you should get the gist of it :)
I have a special url which I would want only few people to have access to. I have a list of super users stored as an array in the app.yml. How can I use this array in :requirements section of a specific route in the routes.rb file to allow only those super users access to this route? Thanks a lot.
No, you can't. :requirements are related to route parameters only.
Which is, in my opinion, a good thing. It's a well known convention to have authentication logic in controllers.
Like Pedro said.. authentication logic should be in controller code.
Take a look at before_filters, where you specify methods that will be called before (all or specified) actions in a controller are run. You can use such a method to deny actions from running. Look for the section named Filter Chain Halting here
:requirements is for specifying constraints for parts of the URL for the route to match. Usually regular expressions are specified as shown here.
map.geocode 'geocode/:postalcode', :controller => 'geocode',
:action => 'show', :requirements => { :postalcode => /\d{5}(-\d{4})?/ }