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:
- Have a "Routing" controller / use slugs
- 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