开发者

Changing params in Rails 3 routes

On one of our controllers, we're getting incoming Google traffic to an invalid URL param:

/search/?klass=forum

Instead, it should be:

/search/?klass=forums

Currently I have some controller#index code that does this:

    if params[:klass] == "forum"
        params.delete(:action) 开发者_如何学Go&& params.delete(:controller)
        params[:klass] = "forums"
        @query = params.to_query
        redirect_to "/search?#{@query}", :status => 301 and return
    end

I'm curious if this is possible to do in routes.rb so it doesn't hit our stack but still properly 301's. I've seen regex's used to validate certain params in the query string, but none used to rewrite the values of those params.


You can try to handle it with nginx or apache, or you can use this hacky solution using Rack:

get "/search/" =>  proc { |env| Rack::Request.new(env).params['klass'] == "forum" ? [ 302, {'Location'=> "/search/?klass=forums" }, [] ] : [ 302, {'Location'=> "/" }, [] ] }


Maybe you could declare this route with :constraints => {:klass => 'forum'} and forward it with the correct value for klass. Or why don't you just append the missing 's' in the controller?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜