在 Rails 中,将带有'renamed params'的旧网址重定向到新网址



我的应用程序旧URL是(在php)

www.abc.com/athlete/?country=2

现在新URL是(在Rails中):

www.asdf.com/athletes/?c=2

现在,我需要将旧URL重定向到新URL。通过config/routes.rb或通过将一个Vertual动作重定向到

的控制器中的重定向
def redirect_to_index
   redirect_to :action => :index, :c => params[:country], :status => 301
end

但是问题是有许多旧的URL没有参数,即无country。但是在重新效果时,我将始终有?c=

我不想使用

之类的粗糙代码
def redirect_to_index
   if params[:country]
     redirect_to :action => :index, :c => params[:country], :status => 301
   else
    redirect_to :action => :index, :status => 301
   end
end

有更好的解决方案?

您可以做这样的事情:

new_params = {
  :c => params[:country],
  :x => params[:something_else]
}.reject{|k,v| v.nil? }
redirect_to ({ :action => :index, :status => 301 }.merge(new_params))

最新更新