S
S
Stamm2012-09-06 21:44:07
Ruby on Rails
Stamm, 2012-09-06 21:44:07

How to make an exception in routing in rails 3

There is a routing specified at the very end

match "/:url" => "main#article", as: :article


I have a static favicon.ico file.
I run the project locally without nginx (with it, all this is implemented through location = /favicon.ico).
The browser requests the icon and the request is routed to main#article.
How to create an exception in routing so that the query set goes to static?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ilya_Drey, 2012-09-06
@Ilya_Drey

It's pretty simple, routes.rb:

match "/favicon.ico" => "tools#favicon"
match "/:url" => "main#article", as: :article, constraints: DoesntMatch.new(path:

tools_controller.rb
class ToolsController < ApplicationController
  def favicon
    data = File.open("public/favicon_test.ico", "rb").read
    send_data(data, :filename => "favicon.ico", :type => "image/png")
  end
end

D
dizer, 2012-09-06
@dizer

not to bring such requests to the app-server and give them to the proxy server?

S
sl_bug, 2012-09-06
@sl_bug

   class DoesntMatch                                                                                 
     def initialize(options)                                                                         
       @options = options                                                                            
     end                                                                                             
                                                                                                     
     def matches?(request)                                                                           
       @options.inject(true) { |m, (k,v)|                                                            
         m && (request.send(k) !~ v)                                                                 
       }                                                                                             
     end                                                                                             
   end 

  match "/:url" => "main#article", as: :article, constraints: DoesntMatch.new(path: /^\/favicon\.ico/)

like this

S
Stamm, 2012-09-09
@Stamm

It will probably be easier to raise nginx + unicorn than to fence such bicycles, which is a pity =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question