If I'm in a URL such as
http://domain.example/mysite/bla
How can I request just the URL with no paths? Such as
http://domain.example
You can use this
<%= request.protocol + request.host_with_port %>
#=> https://domain.example:3000
<%= request.protocol + request.host %>
#=> https://domain.example
Starting from Rails 3.2 you can also use
<%= request.base_url %>
#=> https://domain.example:3000
request.host should do the trick, or:
request.port.blank? ? request.host : "#{request.host}: #{request.port}"
if you need to include the port too.
Try this
<%=request.scheme + '://' + request.host_with_port%>
If you want to see all available methods on request object then
<%=request.methods.sort%>
For protocol, domain and port
<%= "#{request.protocol + request.host}:#{request.port.to_s}" %>
Here's how you can get your current domain name.
Domain name
request.base_url
# prod: https://example.com
# dev: https://localhost:8000
Getting full path
request.original_url
# prod: https://example.com/path/
# dev: https://localhost:8000/path/
If your port could be anything other than 80 it should be included.
"#{request.protocol}#{request.host_with_port}"
I think this can be useful too, if you're not in a controller.
URI.join(url_for(only_path: false), '/').to_s
Related
I have an angular file set up with my view template urls that gets used in my app when setting up routes. One of the urls will change based on the rails environment and I don't always know at least for the staging environment, what the url will be. So I was hoping to use rails request.host_with_port but when I run it I get the error
undefined local variable or method `request'
Here's the templates.js.erb file. Is there another way I can do this? Thanks.
angular.module('jobBoard')
.constant('templates', {
apply : '<%= asset_path('apply.html') %>',
detail : '<%= asset_path('detail.html') %>',
<% if Rails.env.development? || Rails.env.test? %>
apiUrl: 'http://localhost:3000/api'
<% elsif Rails.env.staging? %>
apiUrl: <%=request.host_with_port%>'/api'
<% else %>
apiUrl: 'http://www.knownurl.com/api'
<% end %>
});
I'm parsing a URL in my Rails application. I want to get the domain example.com stripped of any protocol, subdomain, directories, etc.
My method:
def clean_host(url)
uri = URI.parse(url)
return uri
end
What I actually get for example.com/ is:
scheme:
user:
password:
host:
port:
path: example.com/
query:
opaque:
registry:
fragment:
parser:
What I eventually want is to get the domain example.com stripped of any protocol, subdomain, directories, etc.
I've looked into domainatrix but it wont bundle with my project. Is there a way to do this with Ruby's URI.parse? Or will I have to look into other avenues?
The problem is that example.com/ isn't a full URL. URI rightly assumes it's a path. If it were me and you're going to have a lot of these fragments I'd just brute force it.
> "example.com/".sub(%r{^.*?://}, '').sub(%r{/.*$}, '')
=> "example.com"
> "http://subdomain.example.com/path/here".sub(%r{^.*?://}, '').sub(%r{/.*$}, '')
=> "subdomain.example.com"
Stripping the subdomain off is another ball of wax as you'd need to example the TLD to determine what is appropriate so you don't end up with say "com.uk"
You should just be able to do request.domain assuming you are doing this in a controller.
You can also use request.path and request.subdomain
For example: http://www.domain.com/users/123
request.subdomain => 'www'
request.domain => 'domain.com'
request.path => '/users/123'
I am trying to check if a file exists so that I can either display the image or a placeholder but the placeholder is always shown. If the conditional statement is removed then the logo is displayed fine.
<% if File.exists?(Rails.root + '/public/images/portal/logos/' + #organisation_id + '.png') %>
<img src="/images/portal/logos/<%= #organisation_id %>.png" alt="<%= #person.organisation.name %>">
<% else %>
<img src="http://placehold.it/300x83&text=Please+upload+your+company+logo">
<% end %>
I've read a few questions but most seem to relate to Rails 3 but seeing as I don't get any errors I thought this would work.
Rails.root is working in rails 2? it may be RAILS_ROOT
Rails.root returns a Pathname. Adding an absolute path to a Pathname removes the existing path in the Pathname.
Ie.
Rails.root #=> #<Pathname:/foo/bar>
Rails.root + "baz" #=> #<Pathname:/foo/bar/baz>
Rails.root + "/baz" #=> #<Pathname:/baz>
If you do
Rails.root + 'public/images/portal/logos/#{#organisation_id}.png'
it should work. Or perhaps even better:
Rails.root.join("public/images/portal/logos", "#{#organisation_id.png')
Compare this to RAILS_ROOT which returns a String:
RAILS_ROOT #=> "/foo/bar"
RAILS_ROOT + "baz" #=> "/foo/barbaz"
RAILS_ROOT + "/baz" #=> "/foo/bar/baz"
When using link_to:
<%= link_to('View', group_url(136, :only_path => false)) %>
You can use only_path to get a url output with http://www.site.com/groups/1
Where without only_path, you get /groups/1
How can I get just the path in rails? outside of a link_to? How do you get: http://www.site.com
Reason why is I want to create hash deep linked URLs, so I'm thinking about doing:
<%= link_to('View', XXXXXX + "#" +group_url(136, :only_path => true)) %>
Where XXXXXX is the url, http://www.site.com
Which would then give me what I want:
http://www.site.com/#/groups/1
Thanks
request.host to return host ("www.your_site.com")
request.protocol to return protocol, for example http:// or https//
request.host_with_port to return port with host ("www.your_site.com:3456")
<%= link_to('View', request.host + "#" +group_url(136, :only_path => true)) %>
<%= link_to('View', request.protocol + request.host + "#" +group_url(136, :only_path => true)) %>
<%= link_to('View', request.protocol + request.host_with_port + "#" +group_url(136, :only_path => true)) %>
EDIT
hack:
host = group_url(136).gsub(/#{group_url(136, :only_path => true)}/, "")
or
url = group_url(136)
path = group_path(136)
host = url.gsub(/#{path}/, "")
In a controller, request.env['HTTP_HOST'] will contain the host part of the request that came in, so you could create a helper method to return a properly formatted "base url" doing something like this:
def site_root_url
if request.env['HTTPS'] == 'on'
proto = 'https'
else
proto = 'http'
end
"#{proto}://#{request.env['HTTP_HOST']}"
end
I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc).
I'm using:
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
in my view
Part b:
In development mode Rails gripes if I don't specify the host explicilty in the link above. But I don't want to do this in production. Any solutions?
To solve the problem to pass a host for generating URLs in ActionMailer, check out this plugin and the reason why I wrote it.
To solve the first issue, use named routes when applicable. Instead of
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
assuming the route is called login, use
<%= login_url(:guid => #user.new_user.guid) %>
Note, I'm using login_url, not login_path.
I'm not sure if it is what you want but in config/environments/development.rb you can specify default options for mailer urls
config.action_mailer.default_url_options = {
:host => "your.host.org",
:port => 3000
}
you can do the same in config/environments/production.rb
I don't know why the previous solutions seem so complicated, but since I'm here why not give my 2 cents...
Go to /config/environments and add:
config.absolute_site_url = 'your site url'
for the respective environment (ie. in development.rb, test.rb, or production.rb). Restart web server.
This allows you to call Rails.application.config.absolute_site_url to get the desired URL. No need for plugins or weird cheat, just store the site url as an application wide variable.
I think its not 100% correct way but this can also be a solution :
See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}/login/?guid=#{#user.new_user.guid}"
To generate url, try this
Rails.application.routes.url_helpers.user_url(User.first.id, host: 'yourhost.io')
this will generate url like this:
http://yourhost.io/users/1
As well you can pass some params
expires = Time.now + 2.days
params = {expires: expires}
u = User.first.id
Rails.application.routes.url_helpers.user_url(u, params, host: 'host.com')
will generate:
http://yourhost.io/users/1.expires=2018-08-12+15%253A52%253A15+%252B0300
so you can werifi in action if link is not expired