what to do to clear the query string in url ? Is redirection the only way?

1.7k views Asked by At

My app is in rails .

I have a Get browser request coming(when user clicks a link in another website) to my app with important query string parameters like http://example.com?logintoken=hjhzjkdhz .

But I feel that I should use this logintoken in my controller but don't want to show that to user on the browser url bar after rendering the page . so in short I want to show only http://example.com in url bar of browser finally.

Is there a way to just clear the query string ?

I see that redirection to http://example.com at the end is one way to achieve this . Is there any other way ?

2

There are 2 answers

0
Ruby Racer On

You can't do that without redirecting... Well, technically you can, if you manipulate browsers states after render (history push and pop).

On the rails side, you will need to redirect and pass your URL parameter to a server variable and redirect to the action, without the params, using these variables:

def myaction
    if params.any?
        session[:myaction_params]={}
        session[:myaction_params]=params
        redirect_to myaction_path
    elsif session[:myaction_params] && session[:myaction_params].any?
        # use your params like this
        # params[:logintoken] will now be session[:myaction_params][:logintoken]
        # apply logic here
    else
        # do something else if there are no params
        # redirect_to somewhere
    end    
end
0
fnln On

An alternative way to do this is to change your request to a POST and so hide the params in the message.