Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails routing - redirection with block AND status code

Well, I've been hitting my head against a brick wall with this one - any help much appreciated!

I'm redirecting old urls and for the most part that's easy and working well, e.g.:

match '/pages/holiday-specials/', :to => redirect( "/accommodation", :status => 301 )

However, I need a special catch-all rule that needs to do some regex checking. This works well EXCEPT I can't get it to pass the status. This redirects and does what I need but doesn't send the 301 status:

match '/*:path', :to => redirect( lambda { |params| "/operator/#{/[^\d](\d+)([^\d]|$)/.match(params[:path])[1]}" }, :status => 301)

Any ideas?

like image 579
Drayton Miles Avatar asked Aug 09 '12 09:08

Drayton Miles


1 Answers

Move the block outside the method parentheses, as follows:

match '/*:path', :to => redirect(:status => 301) { |params| "/operator/#{/[^\d](\d+)([^\d]|$)/.match(params[:path])[1]}" }

or split it over multiple lines:

match '/*:path', :to => redirect(:status => 301) do |params|
  "/operator/#{/[^\d](\d+)([^\d]|$)/.match(params[:path])[1]}"
end
like image 112
lime Avatar answered Oct 19 '22 18:10

lime