Search code examples
ruby-on-railsrubyruby-on-rails-3urlslash

How to generate links with trailing slash in Rails 3?


I'm porting the existing website from PHP to Ruby on Rails 3 and I have to keep the urls unchanged.

I have the route:

get 'companies/' => 'companies#index', :as => :companies

In a view file I have:

link_to 'Companies', companies_path

and this generates the url "http://website.com/companies" instead of "http://website.com/companies/".

I want the slash at the end of the url. Is it possible?


Solution

  • You can add this to your application.rb:

    config.action_controller.default_url_options = { :trailing_slash => true }
    

    This way all routes will be generated with a trailing slash automatically, with no need to modify each link in your project.