Turning Slash Behavior into a Rails Application

I am currently trying to simulate the behavior of a folder / files in rails using a category / article scheme. So, I have this in the routes:

 match '/:category/' => 'category#list_articles'
 match '/:category/:article.:format' => 'article#show'

Basically, request the urls:

http://www.example.com/category/
http://www.example.com/category/article.html

Everything is working. The problem is that it works well. A URL, for example http://www.example.com/category(without a trailing slash), also serves in the list of articles. Is there a way to block this from 404 or is it better to redirect to a category with a trailing slash?

Using Rails 3, nginx, ruby ​​1.9.2. Thank!

+1
source share
1 answer

, -, , :

class TrailingSlashes                                                                                                      
  def initialize(app)
    @app = app
  end

  def call(env)
    if match = env['REQUEST_PATH'].match(/(.*)\/$/)
      response = Rack::Response.new
      response.redirect(match[1])
      response
    else
      @app.call(env)
    end
  end
end
+2

Source: https://habr.com/ru/post/1546905/


All Articles