Sahil Dhankhar
Sahil Dhankhar

Reputation: 3656

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 ?

Upvotes: 0

Views: 1744

Answers (2)

Aleks
Aleks

Reputation: 5320

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

Upvotes: 1

sjain
sjain

Reputation: 23344

Although it is not a good practice to use a get request instead of post but as per the requirements that you have there is just a hack way to get escape the special characters as follows:

require 'uri'
tip_topic_name= URI.escape(tip_topic_name)
url = "http://myurl.com/#{tip_topic_name}"
result = open(url).read

Also check the docs that have escape representation.

Upvotes: 0

Related Questions