Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current action's path/url including query string? (Rails)

Simple question - how do I get the path or full URL of the current action INCLUDING the query string?

I wish to save it to the session variable like so:

def show
  @thingy = Thingy.find(params[:id])

  session[:some_var] = current_url

  ...
end

At the moment I'm doing the following, but it seems a bit heavy-handed (especially the specifying of query string params individually):

def show
  @thingy = Thingy.find(params[:id])

  session[:some_var] = thingy_path(@thingy, :q1 => params[:q1], :q2 => params[:q2])

  ...
end
like image 859
XåpplI'-I0llwlg'I - Avatar asked Aug 24 '12 08:08

XåpplI'-I0llwlg'I -


People also ask

What is the best way to get the current request url in Rails?

What is the best way to get the current request URL in Rails? You should use request. original_url to get the current URL.

How can you get the current absolute url in your Ruby on Rails view?

You can write request. url instead of request. request_uri . This combines the protocol (usually http://) with the host, and request_uri to give you the full address.

How do I get the current url in Ruby?

You should use request. original_url to get the current URL. For Rails 3: You can write "#{request.


2 Answers

access params variable,it will give you query as well as controller and action. By using request object you can dig more deeper if you want.

like image 89
Amar Avatar answered Sep 21 '22 22:09

Amar


request.url is probably what you are looking for.

like image 43
Petter Wigle Avatar answered Sep 17 '22 22:09

Petter Wigle