Question

I've tried adding this in application.rb

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

as well as having :trailing_slash => true in routes.rb

match '/download', to: 'welcome#download', via: 'get', :trailing_slash => true

But neither seems to work. I searched through rails 4.0 doc but couldn't find related info. What am I missing here?

Update:

I've tried adding

Rails.application.default_url_options[:trailing_slash] = true

in filter_parameter_logging.rb since this is the only place in the whole project where I could find Rails.application.*, but it's not working either. I found the line here among the releases and I am using 4.0.4. Am I adding this in the wrong place? And I did restarted server before rechecking.

And sorry for the simple question but from what I've gathered isn't trailing_slash supposed to be reflected in browser url as well, if not primarily? Because this is what I need, to go with historyjs.

Was it helpful?

Solution

I think you have the meaning of :trailing_slash => true wrong.

All it does is add the / to the end of you path helpers. No redirecting involved.

Your routes will still respond to both with and without the trailing slash.

If you want to redirect all non-trailing_slash uri's like /download to /download/ using a nginx http server you would do something like this:

rewrite ^([^.\?]*[^/])$ $1/ permanent;

You would still want to add the :trailing_slash => true to your routes so your path/url helpers generate the the correct uri's (so user don’t need to redirect).

OTHER TIPS

Trailing_slash refers to a / after the name like page/ not like /page.

You have given your routes wrongly.

Change it to

match 'download/', to: 'welcome#download', via: 'get', :trailing_slash => true

There is also other way to achieve this by giving a trailing_slash => true option directly to your link_to helper.

link_to 'Downloads', downloads_path(:trailing_slash => true)

Though this work in Rails 3,not sure about Rails 4.

For more details see this SO.

I am using rails 4.0.2 for me it's working

routes.rb

       get 'admin/update_price_qty' => 'admin#update_price_qty', :trailing_slash => true,:as  => "price"

in console :-

     irb(main):003:0* app.price_path
     => "/admin/update_price_qty/"

routes.rb

   match '/download', to: 'welcome#index', via: 'get', :trailing_slash => true,:as => "welcome_price"

in console :-

   `irb(main):002:0> app.welcome_price_path
    => "/download/"`

But I've tried adding this in application.rb

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

not working.

You may add this line to config/application.rb:

config.action_controller.default_url_options = { trailing_slash: true }

If you do this, when you call a Rails path helper inside a controller or helper, the generated path will have a / at the end:

class ApplicationController
  def index
    download_path # returns "/download/"
  end
end

module PathHelper
  def path
    download_path # returns "/download/"
  end
end

If you need to use path helpers outside controllers and helpers, you need to include Rails.application.routes.url_helpers, but apparently, this ignores the trailing_slash configuration above:

class SomeClass
  include Rails.application.routes.url_helpers

  def path
    download_path # returns "/download"
  end
end

In this case, you should add { trailing_slash: true } as a parameter:

class SomeClass
  include Rails.application.routes.url_helpers

  def path
    download_path(trailing_slash: true) # returns "/download/"
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top