Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a trailing_slash to all urls without in Rails 4?

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.

like image 867
Lucia Avatar asked Dec 14 '22 23:12

Lucia


1 Answers

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).

like image 162
complistic Avatar answered Dec 28 '22 06:12

complistic