Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How to override the 'show' route of a resource?

Currently I have a route that looks like this:

resources :posts

I want to override the 'show' action so that I can display a url like this:

posts/:id/:slug

I am currently able to do this by adding a custom match route:

resources :posts
match 'posts/:id/:slug' => 'posts#show'

However, when I use the link_to helper, it does not use my custom show route.

<%= link_to 'show', post %>  # renders /posts/123

How can I define my show route so that I can still use the link_to helper?

Update: As you can read in the following answers, you can override the route to the 'show' action, but it's probably more work than it's worth. It's easier to just create a custom route:

# config/routes.rb
match 'posts/:id/:slug' => 'posts#show', as: 'post_seo'

# app/views/posts/index.html.erb
<%= link_to post.title, post_seo_path(post.id, post.slug) %>
like image 954
Andrew Avatar asked Jul 19 '12 23:07

Andrew


People also ask

What is rails routing in Ruby on rails?

Ruby on Rails routes are essential to a Rails application’s function. The Rails routing system checks the URLs of incoming requests and decides what action or actions the application should take in response. The Rails router accomplishes this task by following the rules that the developer specifies in the configuration file config/routes.rb.

What is resource routing in rails 2?

2 Resource Routing: the Rails Default. Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

How do I use resources in rails?

A single call to resources can declare all of the necessary routes for your index, show, new, edit, create, update, and destroy actions. Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as GET, POST, PATCH, PUT and DELETE. Each method is a request to perform an operation on the resource.

What are routes in rails?

Rails Cookbook - Advanced rails recipes/learnings and coding techniques Routes are defined in config/routes.rb. They are often defined as a group of related routes, using the resources or resource methods. resources :users creates the following seven routes, all mapping to actions of UsersController:


2 Answers

You have two routes which point to posts#show (you should be able to confirm this by running rake routes), and your link is using the wrong one.

When you call link_to('show', post) the URL of the link is generated by calling url_for(post) which (eventually, after passing through several other methods on the way) calls post_path(post). Since the route to posts#show that was created by your call to resources(:posts) is named post, that is the route that post_path generates.

You also currently have inconsistent routes for the show, update and destroy actions which will probably cause you problems later on.

You can fix this by changing your routes to the following:

resources :posts, :except => ['show', 'update', 'destroy']
get    'posts/:id/:slug' => 'posts#show', :as => 'post'
put    'posts/:id/:slug' => 'posts#update'
delete 'posts/:id/:slug' => 'posts#destroy'

Unfortunately you still can't use link_to('show', post) just yet, because it relies on being able to use post.to_param as the single argument needed to build a path to a post. Your custom route requires two arguments, an id and a slug. So now your link code will need to look like this:

link_to 'show', post_path(post.id, post.slug)

You can get around that problem by defining your own post_path and post_url helpers in app/helpers/posts_helper.rb:

module PostsHelper
  def post_path(post, options={})
    post_url(post, options.merge(:only_path => true))
  end

  def post_url(post, options={})
    url_for(options.merge(:controller => 'posts', :action => 'show',
                          :id => post.id, :slug => post.slug))
  end
end

Which means we're finally able to use:

link_to 'show', post

If that all seems like too much work, a common alternative is to use URLs that look more like posts/:id-:slug, in which case you can stick with the standard RESTful routes and just override the to_param method in your Post class:

def to_param
  "#{id}-#{slug}"
end

You'll also need to do a little bit of work splitting up params[:id] into an ID and a slug before you can look up the relevant instance in your show, edit, update and destroy controller actions.

like image 108
georgebrock Avatar answered Sep 30 '22 16:09

georgebrock


resources :posts, except: :show do
    get ":slug" => :show, as: "", on: :member
end

and define helper

  def post_path post
     "/posts/#{post.id}/#{post.slug}"
  end
like image 42
Yuri Barbashov Avatar answered Sep 30 '22 16:09

Yuri Barbashov