0

I have the Articles controller and for displaying the respective article I use the basic routes - example.com/articles/4.

I would like to change this URL format to example.com/4-article-name or example.com/article-name-4.

Could anyone give me a tip, how to do that?

Thanks

user984621
  • 41,002
  • 66
  • 200
  • 371

2 Answers2

1

Ryan Bates(Railscast.com) seems to have done an episode that solves your problem:

/app/models/article.rb 
class Article < ActiveRecord::Base
  def to_param
    "#{id} #{name}".parameterize
  end
end

See: http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

knsmr
  • 281
  • 1
  • 9
1

Generally numbers on the beginning is the most elegant way, since in ruby it could be very easily converted to integer, for instance '123-foo-bar'.to_i will return 123. See http://apidock.com/rails/ActiveRecord/Base/to_param you'll find how to change this mechanism.

luacassus
  • 6,206
  • 2
  • 38
  • 57