Lets assume my RoR app lives at
http://case-1.example.com/part-1/sub-part-1/
In a view file, how can I get that path?
To get full url of the current page you can use
current_url = request.request_uri
This will return you full url, you can then gsub http or https.
Hope this was you were looking for.
Thanks..
For rails 3+
Use :
#app_root_path = Rails.root
For rails < 3
Use
#app_root_path = RAILS_ROOT
You can get the path by following:-
#rails_app_path = RAILS_ROOT
Thanks......
Related
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"
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.
is possible to generate an HTML page using Ruby on Rails framework without using a webserver?
I want do something like this:
html = RailsHTMLGenerator.generate('path/to/rails/root', '/posts/540')
puts html
The first parameter is the Rails.root, the second is the HTTP path, and the function return the HTML of that page as string.
Someone can tell me how to do this? Ty.
I've found a solution:
require '/path/to/application.rb'
app = APPName::Application.initialize!
session = ActionDispatch::Integration::Session.new(app)
session.get '/'
puts session.body
Well, you can do that from the Rails console
app.get '/foo'
This is how the console is initialized in case you want to try this approach:
https://github.com/rails/rails/blob/master/railties/lib/rails/commands/console.rb
I need to print Ruby on Rails complete url in my application. in details
with RAILS_ROOT I m getting a url like this
D:/projects/rails_app/projectname/data/default.jpg
But for my application I need a path like this
http://localhost:3000/data/default.jpg
Please help me to solve this issue. I am using Rails 2
Thanks
Today we use URI. Simply require the library and you will be able to parse your current dynamic and static URI any way you please. For example I have a function that can read URI parameters like so...
#{RAILS_ROOT}/app/helpers/application_helper.rb (The literal path string of the file depicted below)
def read_uri(parameter)
require 'uri'
#raw_uri = URI.parse(request.original_fullpath)
#uri_params_raw = #raw_uri.query
if #uri_params_raw =~ /\=/
#uri_vars = #uri_params_raw.split('=')
return #uri_vars[parameter]
end
return false
end
This should split all URI parameters into an array that gives the requested (numeric) "parameter".
I believe that simply the URI.parse(request.original_fullpath) should work for you.
I come from using a minimum of rails 4.2.6 with this method so, I hope it works for anyone who might view this later on. Oh, and just as a disclaimer: I wasn't so wise to rails at the time of posting this.
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.