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 "" instead of "".

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

-------------Problems Reply------------

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.

Simply do as follows:

link_to 'Companies', companies_path(:trailing_slash => true)

Documentation here.

I couldn't find any references, but adding trainling_slash: true to the route definition also works (and avoids repeating oneself).

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

This was tested with Rails 3.2.13:

rails c
1.9.3p327 :005 > app.companies_path
=> "

For rails 3.2:

Rails.application.routes.default_url_options[:trailing_slash]= true

Category:ruby on rails Views:0 Time:2011-06-26

Related post

  • Redirect to canonical route in without trailing slash in Rails 3 2011-12-21

    On Rails 3, I'm trying to redirect from a URL without a trailing slash to the canonical URL that has a slash. match "/test", :to => redirect("/test/") However, the route above matches both /test and /test/ causing a redirect loop. How do I make it

  • How to make Rails do not ignore trailing slashes in the routes? 2011-06-26

    For example, I have the rote in routes.rb: get 'companies/' => 'companies#index', :as => :companies There is the way to generate link with the trailing slash (like ""): link_to 'Compaines', companies_path(:trailing_

  • Trailing slashes on some URLs, but not others - good practice? Should I redirect? 2012-02-05

    I've been looking into standardizing all of my sites URLs before I launch (For obvious reasons), and have been doing a lot of research on the subject. I've come to the following conclusions: It does not matter if you use a forced trailing slash or fo

  • Echo A Link, Get A Trailing Slash? 2008-10-01

    I've discovered that any time I do the following: echo '<a href="http://" title="bla">huzzah</a>'; I end up with the following being rendered to the browser: <a href="http:///" title="bla">huzzah</a> This is particularly annoy

  • Find all the links in the database that do not have a trailing slash 2011-12-19

    There are some links in my database that do not have a trailing slash, and for consistency sake, I want all links to have one. All the links are in this form href="" Now there are some links that look like this href="http:

  • Add a trailing slash at the end of each url? 2009-09-06

    I have a little problem here. I need to add a trailing slash at the end of each url in the site I'm working on. I defined all the links inside the site to have a trailing slash like so: <a href="/register/">Register</a> While this works f

  • How to get a trailing slash appended to page routes? 2010-11-11

    This is a near identical problem I am having to that of this query, albeit mine is a Web Forms scenario (using routing in .NET 4) as opposed to MVC. Add a trailing slash at the end of each url? The solution that someone mentions there is only half pr

  • HTaccess - Trailing slash + .php + anchors (ExpressionEngine) 2011-07-14

    I just started a project from another webmaster work (i hate patching other's stuff ::sad::) and i have some issues. First we are on expression engine 1.x . My problem: There is a trailing slash redirection in the .HTACCESS, but my users need to have

  • How to add a trailing slash on URL 2011-09-16

    I would like all URLs on my site to have a trailing slash on the URL. I created a simple extension for the URLHelper in Zend. Right now it changes all the word separators to hyphens (-). Adding the functionality to append a trailing slash would be gr

  • mod_rewrite trailing slash for directory and remove for file url 2012-03-10

    Ok. I got this problem I trying to remove the last slash in a file url for example I got the code for adding slash to the end but cannot figure how to do the conditional. If the URL has an extesion then remove en

  • What is the significance of trailing slashes in a namespace URI? 2009-01-10

    I have been studying SOAP and WSDL in preparation for implementing a web service. One thing that I have encountered that puzzles me is that some of the URIs that I have seen use a trailing slash such as: while other

  • Is it always safe to remove a trailing slash from a URL? 2009-06-03

    I'm storing URLs in a database, and I want to be able to know if two URLs are identical. Generally, a trailing slash at the end doesn't change the response you'd get from a server. (ie. is the same as Can

  • Why do so many Ruby on Rails apps have missing trailing slashes? 2009-10-23

    Why do so many Ruby on Rails apps have missing trailing slashes in their URLs? One example is AFAIK this goes against Web standards. Is it something to do with the way RoR is set up? --------------Solutions------------- It

  • Code Igniter url routing question (using trailing slashes) 2009-11-16

    I am brand new to codeIgniter. I am very particular about urls on the sites that I develop. Is it possible to create these sorts of urls? Generally sites I develop have an integrated admin interface as well with new, edit or delete added onto the end

  • Trailing slash in URL causing Partial Post Back issues 2009-11-16

    .NET 3.5 app written in C# here with both jQuery and some ASP.NET AJAX UpdatePanel flavouring. I'm running into an interesting issue. I created a pagination user control that is made up of LinkButtons. The user control fires off an event called Curre

  • ASP.NET MVC + IIS7 + FireFox : trailing slash in URL 2010-01-26

    Note: I have done hours of digging for the answer, and couldn't find one. I have an ASP.NET MVC (2.0) application, hosted on IIS7 (integrated mode). When GET request is made to /Toons/List - I get a redirect (302) to /Toons/List/ which is expected. T

  • End my urls with trailing slash (/) or not? And how do I go about it doing this with codeigniter 2010-03-05

    Hey guys, I am trying to figure out which is most appropriate. From the articles I have read, it seems best to end url's with a trailing slash. So instead of: It would read: First I adjusted my

  • ASP.NET - Trailing Slash and Tilde 2010-06-16

    I've found what seems like a bizarre problem with IIS 6.0 and .NET 3.5. I always use the tilde for all URLs (eg: ~/mypage.aspx) so if I go to, everything works fine. If, however, I add a trailing slash and go to

  • Trailing slashes: yes or no? 2010-06-17

    This question already has an answer here: Trailing slash in URLs - which style is preferred? [closed] 8 answers I'm working on a new project, using Apache's mod_rewrite and PHP to get pretty URLs. Here's what my URLs look like: http://example.tld/foo

Copyright (C), All Rights Reserved.

processed in 0.106 (s). 11 q(s)