Get hostname of URL in ruby on rails - 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"

Related

Ruby on Rails how to get current URL?

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.

How do I get the host url in mail template

I have a mailer template in rails. I am trying to use the url of a paperclip attachment: <%=item.picture.url%>
OFcourse, this only renders the path which is pretty useless in an email. I need to get the absolute url.
Also since I am using rake to run the task, there is no request per se.
I remember setting the default host in
config.action_mailer.default_url_options = { :host => 'sitename.com' }
How do I access this value in the mailer template?
you can use helper in action-mailer and absolute url helper is root_url.
Try this
You can get the host name using the below line of code
request.host

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 do I write a full path in a controller in Rails 3?

I need to write the full path so need to know what the rails_root domain is. How do I do that? For example:
string = "{RAILS_ROOT}/vendors/#{#vendor.id}"
What is the equivalent of "RAILS_ROOT" to give me what the full domain is for my application? So that in development it would subsstitute localhost:3000 and on my heroku site the right full domain?
You should always avoid, if possible, hard-coding your path, because it is less flexible and more prone to result in broken links in the future. Plus, you can use Rails routing, which is an elegant way to generate everything cohesively in Rails without any need to create the composite parts yourself.
If you have your routes set up properly, you should be able to call:
link_to "View vendor", vendor_url(#vendor.id)
Vendor_url(#vendor.id) in Rails gives you your full URL, which you can then contain in your string variable. Here's how to generate the routes needed for the above:
# in routes.rb
resources :vendors
Try:
File.realpath(RAILS_ROOT)
You could access the request object. request.host_with_port would give you the hostname and port. request.protocol will give you the protocol (http:// or https://). request.fullpath will give you the path with query params.

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