Rails routing - redirection with block AND status code

Viewed 1423

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?

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
Related