0

i have routes like this :

get "/:article_id" => "categories#show", as: :articles_category
get '/:account_id' => "accounts#show", as: :show_account

but why when i access show_account_url, i always entry to articles_category_url ??

why?

how to make my routes have twice "/:id" in url with different action?

3lviend
  • 243
  • 5
  • 13
  • 1
    I don't think it is a good idea. Will you be able to differentiate between `xyz.com/15` and `xyz.com/30`, which one is the `show_account_url` and `articles_category_url`. No? When, we being human can't get it then how would we teach the machine to do the same? As simple as that :) – Manoj Monga Apr 21 '14 at 07:53

1 Answers1

0

But why when i access show_account_url, i always entry to articles_category_url ??

The problem you have is you're trying to access the same URL -- domain.com/______. Because Rails cannot process the difference, it uses the first route - your category_url.


There are two ways to deal with this:

  1. Have a "Routing" controller / use slugs
  2. Split your routes up conventionally

Everyone wants app-wide slugs, but you can't do it unless you have a mechanism to calculate which URL is correct. The methods you have to achieve this are either to create a routing controller, or use slugs.

Using a routing controller is actually quite simple:

#config/routes.rb
get "/:id" => "router#direct", as: :slug

#app/controllers/routers_controller.rb
def direct
    #routing code (lots of ifs etc)
end

A better way is to use a slug system, which allows you to route to your slugs directly. We use this with http://firststopcosmeticshop.co.uk & the slugalicious gem:

#Slugs
  begin  
      Slug.all.each do |s|
        begin
          get "#{s.slug}" => "#{s.sluggable_type.downcase.pluralize}#show", :id => s.slug
        rescue
        end
      end
  rescue
  end

This allows you to send specific slugs to specific controllers / actions. The reason? It creates /[slug] routes, which you can access across the site

Further to this, you could look at the friendly_id gem -- which helps you create resourceful routes using slugs. Highly recommended

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147