Rails routing - redirection with block code and status code

Ok, I hit my head against a brick wall with this - any help was greatly appreciated!

I redirect old URLs and, for the most part, easily and efficiently, for example:

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

However, I need a special catch-all rule that should do some regular expression checking. It works well. WITH THE EXCEPTION OF. I can’t get him to convey status. This redirects and does what I need, but does not send status 301:

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

Any ideas?

+5
source share
1 answer

Move the block outside the parentheses of the method, as shown below:

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

or split it into several lines:

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

All Articles