How do I get the current absolute URL in Ruby on Rails?
For Rails 3.2 or Rails 4+
You should use request.original_url
to get the current URL. Source code on current repo found here.
This method is documented at original_url method, but if you're curious, the implementation is:
def original_url
base_url + original_fullpath
end
For Rails 3:
You can write "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
, since request.url
is now deprecated.
For Rails 2:
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 to get the actual HTTP request from a model in rails 3?
You can store the request in the thread, and then access it anywhere.
This is def a hack as you really should not break the MVC convention this way, and if a model is really request dependent you could always pass the request to the model as a parameter.
but the hack to make your request available everywhere would be for application_controller.rb:
before_filter :store_request_in_thread
def store_request_in_thread
Thread.current[:request] = request
end
and in your model somemodel.rb or really anywhere you expect request to already exist, you can just access the request:
def something
request = Thread.current[:request]
end
Rails - How To Get Full URL in Model
helper method is only available in view or decorator.
it's better to define url
in the decorator.
if you want to use helper method in the model, try this
def url
Rails.application.routes.url_helpers.product_url(self)
end
how to get the request uri inside active admin
If you want to have CRUD for your polymorphic nested resources (products/pictures
and services/pictures
), your application needs to have routes like /admin/products/:id/images
and /admin/services/:id/images
. The problem is that when you use belongs_to :parent
in a register
block, active_admin will only generate one nested route admin/parents/:id/child
, whereas you need two. Furthermore, :parent
can't be determine by the current url, because the call belongs_to :parent
itself is used to create the current url (the resource path).
To get around this, you can define the routes yourself in configs.rb
namespace :admin do
resources :services do
resources :pictures
end
resources :products do
resources :pictures
end
end
and tell active_admin to use these routes by writing controller.belongs_to :service, :product, polymorphic: true
in your register
block for Picture
.
Source: https://github.com/gregbell/active_admin/issues/1183
how to get the request uri inside active admin
If you want to have CRUD for your polymorphic nested resources (products/pictures
and services/pictures
), your application needs to have routes like /admin/products/:id/images
and /admin/services/:id/images
. The problem is that when you use belongs_to :parent
in a register
block, active_admin will only generate one nested route admin/parents/:id/child
, whereas you need two. Furthermore, :parent
can't be determine by the current url, because the call belongs_to :parent
itself is used to create the current url (the resource path).
To get around this, you can define the routes yourself in configs.rb
namespace :admin do
resources :services do
resources :pictures
end
resources :products do
resources :pictures
end
end
and tell active_admin to use these routes by writing controller.belongs_to :service, :product, polymorphic: true
in your register
block for Picture
.
Source: https://github.com/gregbell/active_admin/issues/1183
Access request URI in a custom library class in Rails
Assuming the URI is initialized/stored in the same class, you'll have to define it yourself.
If the URL is defined when the class is initialized then create either a class or instance variable. All methods will be able to see it.
Instance variable:
class Blah
def initialize(url)
@url = url
end
def method1
url = @url
end
end
Class variable:
class Blah
@@url = nil
def initialize(url)
@@url = url
end
def method1
url = @@url
end
end
If the URL comes from a different class then you'll have to use whatever accessor that class provides. If there isn't one then you could try something like OtherClass::URL_variable
where you dig up the variable name of the URL by looking in the source or by extending that class to provide an accessor.
Ruby on Rails how to get current URL?
request.original_url
shall provide you current url in rails 4.
You can visit this resource for more info @ How do I get the current absolute URL in Ruby on Rails?
How to retrieve the current URL within a class?
I finally figured it out with some help and debugging. Because I was requesting the URL within a class that was creating a new object in the database, I had to use request.referrer
:
url = request.referrer
uri = URI::parse(url)
id = uri.path.split('/')[2]
And because my URL was like "https://www.example.com/room/4", the id returned a value of 4.
Related Topics
Ruby Concatenate Strings and Add Spaces
Collect Values from an Array of Hashes
How to Put Assertions in Ruby Code
Possible to Alias a Belongs_To Association in Rails
Failed to Build JSON (1.6.3) with Native Extension After I Installed Xcode 4.2
How to Use an Overridden Constant in an Inheritanced Class
How to Configure an Extra/Different Migrations Folder
How to Add a Method to the Global Scope in Ruby
Using Question Mark Character in Rails/Activerecord Column Name
How to Run a Ruby Script Within Bundler Context
Adding a Method to Built-In Class in Rails App
What Are the Reserved Words Begin or End Used for in Ruby
Ruby: What Does the Snippet: (Num & 1) == 0 Exactly Do
Build Hash from Collection of Activerecord Models
How to Use an Actionview::Helper in a Ruby Script, Outside of Rails