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
Related
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'm trying to use Net::HTTP.get_print in a rails app to get the response from a URL, and print the URL on the page. My controller contains:
uri_string = "www.example.com"
uri = URI.parse(uri_string)
#contents = Net::HTTP.get_print(uri)
Then the matching .html.erb file contains:
<%= #contents %>
The resulting page is blank, even when I use a dummy URL like "http://www.google.com". What am I doing wrong? I'm using Ruby 1.8.7 and Rails 3.0.7 (OS X).
nothing is displayed, because get_print outputs to STDOUT and returns nil. Meaning, you'll be able to see the output in your log, but not on your web page.
use #contents = Net::HTTP.get(uri) or similar
I have a rails app in a subdirectory of my server, something like www.domain.com/sub
I need to send an url by e-mail, so I tried to use "resource_url" but it generates a link like www.domain.com/resource_path, where should be wwww.domain.com/sub/resource_path.
How can I solve this ?
Thanks!
In Rails 2.3.8 you can add a line like this to your config/environments/production.rb
ActionController::Base.relative_url_root = "/sub"
I am not sure what the equivalent is for Rails 3, but see this question if that is what you are using:
What is the replacement for ActionController::Base.relative_url_root?
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......
This may be really obvious, but in rails how can I get the contents of a page without making an HTTP request?
i.e. I want to grab the contents of any page that my rails app generates, in a script on the same server. This is of course trivial with an HTTP request, but how can I get the contents of a page otherwise?
This is what I am doing now which works fine:
require 'open-uri'
contents = open("http://localhost/path_to_my_page").read # but I really want to get rid of the overhead of the HTTP request
I need to be able to do this from a script, within the rails app, not a controller (e.g. with render_to_string)
This is how you get the contents of a page in the console. It might work for you:
require 'action_controller/integration'
app = ActionController::Integration::Session.new;
app.get('/path_to_your_page')
puts app.response.body
In your controller , you can call render_to_string instead of render and the page is returned instead of being send to the browser.
The arguments of both methods are the same, so you need to specify which controller and action you require to render.
Why not use erb directly? See this question for more details