Search code examples
ruby-on-railsurlhttp-redirecturl-encoding

rails redirect_to with & in the params fix


I have & in one of my params: redirect action:

format.html {redirect_to :action => 'index',:flag => params[:flag], :tip_type => params[:tip_type], :tip_topic_name => params[:tip_topic_name]}

output:

Started GET "/admin/answer_reviews?flag=tip_filter&tip_topic_name=Flu+&+Cold&tip_type=Haiku" for 127.0.0.1 at 2013-03-08 13:53:38 +0530
Processing by Admin::AnswerReviewsController#index as HTML
Parameters: {"flag"=>"tip_filter", "tip_topic_name"=>"Flu ", " Cold"=>nil, "tip_type"=>"Haiku"}

but i want it to be:

Parameters: {"flag"=>"tip_filter", "tip_topic_name"=>"Flu & Cold", "tip_type"=>"Haiku"}

i have also tried:

format.html {redirect_to :action => 'index',:flag => params[:flag], :tip_type => Rack::Utils.escape(params[:tip_type]), :tip_topic_name => Rack::Utils.escape(params[:tip_topic_name])}

but it leads to:

Started GET "/admin/answer_reviews?flag=tip_filter&tip_topic_name=Flu+%2526+Cold&tip_type=Do%2527s+And+Don%2527ts" for 127.0.0.1 at 2013-03-08 14:01:37 +0530
Processing by Admin::AnswerReviewsController#index as HTML
Parameters: {"flag"=>"tip_filter", "tip_topic_name"=>"Flu %26 Cold", "tip_type"=>"Do%27s And Don%27ts"}

i can gsub '&' with '$' before redirect and then again '$' with '&' in the redirected action , but there must be some less hacky way available ?


Solution

  • Could you try with this:

    :tip_topic_name => raw(params[:tip_topic_name])
    

    EDIT:

    After getting feed back, and my comments below, I think you should go with your approach

    format.html {redirect_to :action => 'index',:flag => params[:flag], :tip_type => Rack::Utils.escape(params[:tip_type]), :tip_topic_name => Rack::Utils.escape(params[:tip_topic_name])}
    

    And then to parse string "Flu %26 Cold" when it is returning to controller with html_safe, but check if it is not nul before that

    you could do that in before_filter , for example, only for index page

    Edit 2:

    What about unescapeing string when parameters are returned. It wouldn't be so hacky.

    Rack::Utils.unescape