Ruby on Rails how to get current URL? - ruby-on-rails

I'm trying to get the current url in rails using request.host, request.url but I have a website that are in 2 domains, so:
when I'm at www.example1.com the request.host or .url show www.example1.com
when I'm at www.example2.com the request.host or .url show example1.com
because the example2 is getting all content from example1 and showing in the example2(parsed)
because the request.host get really the host, and request.url getting the host also
I'm really need the get the url in the browser don't the host, someone can help?
OBs: I'm using rails 4 and ruby 2
OBs2: request.domain don't works, this return just ".com" of 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?

For Rails 4 you should use request.original_url to get the current URL. More detail.

Related

How to get the hostname or entire url of the visitor website in Ruby on Rails

If the user clicks a link on http://facebook.com/my_path/ that points to http://example.com/ the value of request.domain will be example.com
But in my case, I would like to get the visitor's host name facebook.com and entire url http://facebook.com/my_path/
How to get that? Any help would be appreciated. Thanks!
Use request.referer, it is from a library called Rack, which Rails itself uses, more info here https://apidock.com/rails/Rack/Request/referer
Can you try putting debugger in the controller and access that controller in your browser. When you reach the byebug shell, try to check the output of the request variable like below
(byebug) request
#<ActionDispatch::Request:0x007fe49c151840 #env={"rack.version"=>[1, 3], "rack.errors"=>#<
the output should have your referrer path if it exists

How to route a sub-domain to a certain page using ruby on rails

I want to redirect a sub-domain to a certain url using rails
example: login.localhost:3000 to localhost:3000/login
You can do the following in routes.rb to handle the redirect on the rails level:
constraints subdomain: "login" do
get "/login" => redirect { |params| "http://www.myurl.com" }
end
What I was referring to about the server redirects would be setting up a 301 permanent redirect but don't worry you can do it at the rails level too.
for more info on subdomains please watch the following rails casts:
http://railscasts.com/episodes/221-subdomains-in-rails-3
http://railscasts.com/episodes/123-subdomains
Let me know if those steps don't work for you and I will try to assist.

Get hostname of URL in ruby on rails

I am making rails app and I need to get just hostname of my URL from my one of Rails controllers.
If my URL is http://www.example.com/path/0,then I just want to extract www.example.com part. How can I do this? I found request.base_url but this returns http://www.example.com which I do not want.
In javascript there is a function, window.location.hostname. I wonder there is a equivalent in Ruby on Rails.
You can use request.host to get exact your URL which you want.
And you can use request.port to get your port from url
The URI module can do this for you.
uri = URI("http://www.example.com/path/0")
uri.host # => "www.example.com"
I am using Rails 4.2
We can get host and port with one method
request.host_with_port
=> "localhost:3002"

rails get app root/base url

In my app I have a few APIs that under api domain. Now in one of the API I want to generate a url that pointing to the main domain, say
test.com/blabla...
I tried to use url_for but seems the default root_url or request.host is in api domain. Url_for will make it to be
api.test.com/blabla..
while I want it to be
test.com/blabla...
Url_for can take a parameter
host: ...
to set it to be test.com/, the question is how can I get the root/base url (test.com) for host? root_url or request.host are all api.test.com.
Any ideas? Thanks.
Just so that it's useful to someone else , i came across this today
request.base_url
gives the full path in local as well as on live .
request.domain
gives just the domain name so it sometimes kinda breaks the link while redirecting
According to this you can do request.domain
Simplest alternative method:
include in you're class
include Rails.application.routes.url_helpers
create function or just use root_url to get app root/base url:
def add_host_prefix(url)
URI.join(root_url, url).to_s
end
finally: add
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
in:
Your_project_root_deir/config/environments/development.rb
although helpers can be accessible only in views but this is working solution.
request.domain fails on CF it given domain url not base url

How to get the subdomain value from a url?

how can I get the subdomain value in rails, is there a built-in way to do this?
e.g.
test123.example.com
I want the test123 part of the url.
Rails 3.0 has this capability built-in, you can access the subdomain from request.subdomain.
You can also route based on the subdomain:
class SupportSubdomain
def self.matches?(request)
request.subdomain == "support"
end
end
Basecamp::Application.routes do
constraints(SupportSubdomain) do
match "/foo/bar", :to => "foo#bar"
end
end
If you're using 2.3, you'll need to use a plugin such as subdomain-fu.
Use the following method inside your controller
request.subdomains
This Returns an array of subdomains
account_location is also a good plugin. After using it, you can find the account based on different subdomains. And you can find out subdomain from url just by writing request.subdomains(0).first in your code.
In case you are working with a string, and assuming it can be a true URI, you can do this to extract the subdomain.
require 'uri'
uri = URI.parse('http://test123.example.com')
uri.host.split('.').first
=> "test123"
https://stackoverflow.com/a/13243810/3407381
Simple in your controller just do the following
unless request.subdomains.any?
#No domains available redirect
redirect_to subdomain: 'www'
end
You can use the SubdomainFu plugin. This plugin gives you a method current_subdomain which returns the current_subdomain of your app.
You can also have a look at this Railscast
UPDATE
You can also use request.subdomains this will give you an array of subdomains.
For anyone looking to get the subdomains on localhost using WEBrick:
Put config.action_dispatch.tld_length = 0 into config/environments/development.rb and everything should work.
Link to SO post here:
Can I make Rails / WEBrick recognize entries in /etc/hosts as subdomains (instead of domains)?
Link to Github post:
https://github.com/rails/rails/issues/12438
current domain with subdomains:
"#{request.subdomain}.#{request.domain}"
# or
"#{request.subdomains.join(".")}.#{request.domain}"
A bit late to the party but here's what I used in older versions of rails.
subdomain = request.subdomains.join('.')
It should be backwards compatible in newer versions

Resources