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
Related
Just updated a Rails 3.2 application to Rails 4.2 and started getting a funny error. Anytime I try to call link_to with a single argument inside my mailer template, I get an error. However, I can make the same call just fine within one of my regular views.
Inside of my mailer view, I try to call it like this:
# user_mailer/notify.html.haml
...
= link_to "https://example.com"
But when the email gets processed by my job handler, it reports the error ActionView::Template::Error: No route matches {:action=>"index"}. (Interestingly enough, this is what you get when you try to call url_for without any parameters).
However, on my homepage I have no issue using the same thing:
# home/index.html.haml
...
= link_to "https://example.com"
Outputs:
https://example.com
As far as I can tell looking at the docs, nothing changed with link_to between Rails 3.2 and 4.2 so I'm confused why this would stop working... Also confused why it works in one place, but not the other.
link_to with just one parameter doesn't do what you think it does. If you look at the generated link in your view it just points to the site you're currently on. You have to provide two parameters, the body and the url.
= link_to "https://example.com"
# https://example.com
You will need this however
= link_to "https://example.com", "https://example.com"
Trying to get the image full URL inside a view rendered by a mailer.
Is there a proper way in Rails 3+ to get the full url generated by Sprockets?
I know of the request object hack, but since the mailer is invoked inside a rake task,
the request data is not available, obviously.
I'm running Rails 4 beta1 (edge)
In Rails 4 you need to have the following in your production.rb
config.action_controller.asset_host = 'yourdomain.com'
config.action_mailer.asset_host = 'http://yourdomain.com'
Having one with the protocol (http) and one without is deliberate. Maybe it will be different before Rails 4 comes out of beta, but if you have the protocol in action_controller.asset_host then you get urls like http://http://yourdomain.com, and if you don't have the protocol in action_mailer.asset_host it is not recognised as a valid URL because it doesn't validate correctly against an internal Rails Regex.
Then you can use the following in your mailer template:
<%= image_url('mail/awesome.gif', only_path: false) %>
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
This one's got me stumped.
I've got a ruby on rails site here (rails 2.0.2, ruby 1.8.6, running on thins (0.8.2) sitting on top of nginx), and when I hit a certain action I sometimes get the correct response, other times I get a 500 error.
The code in this view goes as follows:
def show
#item = Item.find_by_url_slug(params[:id])
filename = "#{RAILS_ROOT}/public/#{#item.index.url}"
#data = File.open(filename, 'rb') { |f| f.read } # read data via File.
render :layout => false
end
The view is rather simplistic, with show.html.erb containing
<%= #data %>
I've suspected caching, and have tried setting a variety of different headers (cache-control, expires etc), and also tried the expires_now() method, to no avail.
Out of interest I've also tried adding a sleep(2) call in the controller, and looking at the network requests it's obvious on the 500 pages that this method isn't being called - none of the above code in the show action is.
Furthermore, there's no evidence of the call in the production.log when the page doesnt load. So, no errors to debug, just the occasional 500 (happening say 2/4 page loads).
I should add that this code works fine locally, and on our staging server - it's only in production that this is an issue.
Any ideas??
I am generating some json to send to a web service.
Currently I am creating a hash, loading it with the data and then calling to_json to generate the json string to send.
But I figure it would be much cleaner and more rails like if I could use a template in a .erb file to generate the json for me.
All the info I can find on erb files use it to create data to send back to the client. I dont want to do this, I am the client here!
How can I do this?
ERB template engine is something you can use without Rails, actually.
require 'erb'
x = 42
template = ERB.new <<-EOF
The value of x is: <%= x %>
EOF
puts template.result(binding)
In your case you can use a template like
{ foo: <%= model.foo.inspect %>, bar: <%= model.bar.inspect %> }
Store it in a .erb file, read the contents with File.open and then pass it to ERB.new, like in the example.
More info here: http://ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html