mmorava
mmorava

Reputation: 73

I would like to change Rails default redirect code

I'm modifying an existing project, and I want my app to have only status: 301 redirects instead of Rails default 302. As there is a lot of places in the app where redirect_to is used, I would like to skip modifying all of them and adding status: 301 as a parameter, so I would like if possible to change default rails redirect code for my app to be 301, or if that's not possible then somehow "hijack" 302 redirect and change it to 301.

Thanks in advance

Upvotes: 0

Views: 1196

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54714

You could monkeypatch the redirect_to method:

class ApplicationController < ActionController::Base
  def redirect_to_with_permanence(options = {}, response_status = {})
    response_status.reverse_merge! status: :moved_permanently 
    redirect_to_without_permanence options, response_status
  end
  alias_method_chain :redirect_to, :permanence
end

Upvotes: 3

Related Questions