Content for doesn't work in production mode - ruby-on-rails

On one of application's page there is:
<% content_for :head do %>
<%= tag :meta, property: "fb:app_id", content: ENV["FACEBOOK_APP_ID"] %>
<% content_for :title, #check.title %>
<% end %>
And it worked in development, while I was running server at my localhost.
After that I deployed application on heroku. And that simply doesn't work.
What means doesn't work. I load page in development at localhost:
<title>Tenta</title>
<meta content="*****************" property="fb:app_id">
When I load this page deployed at heroku host, these tags are simply absent.
Why?
Also, If you don't know this particular issue solution, I would appreciate if you adivce me, how can I look what is going on - heroku logs and watching last 150 logs in inconvinient windows console doesn't give me a lot of helpful information.

Related

Rails leaflet map geosearch stopped working after precompiling assets

I'm using meijer's geosearch in a leaflet map. It was working fine but I've suddenly broken it and I don't know how. The maps still appears, but the search box doen't appear in the map. The code that breaks the app is (leaflet_map.js):
new L.Control.GeoSearch({
provider: new L.GeoSearch.Provider.OpenStreetMap(),
showMarker: false,
position: 'topright',
retainZoomLevel: false
}).addTo(map);
I don't think it can find the geosearch js or css files, even though I've clearly included them in application.html.erb:
<%= stylesheet_link_tag "leaflet.css" %>
<%= javascript_include_tag "leaflet.js" %>
<%= stylesheet_link_tag "l.geosearch.css" %>
<%= javascript_include_tag "l.control.geosearch.js" %>
<%= javascript_include_tag "l.geosearch.provider.openstreetmap.js" %>
I think the turning point might have been when I typed bundle exec rake assets:precompile at the command line (I also did RAILS_ENV=production bundle exec rake assets:precompile). Why would that break geosearch?
I have deleted my public/assets folder, but that didn't make any difference.
Also, the console doesn't show any complaints about broken geosearch.js files or not finding them. I am, as usual, baffled.
js console:
Uncaught Error: Map container not found.
Uncaught TypeError: Cannot read property 'Provider' of undefined
Mixed Content: The page at 'xxxx' was loaded over HTTPS, but requested an insecure image 'http://a.tile.openstreetmap.org/13/7424/5131.png'. This content should also be served over HTTPS.
view:
<div id="map-leaflet"></div>
<%= javascript_include_tag "easy-button" %>
<%= javascript_include_tag "leaflet_map" %>
leaflet_map.js
map = L.map('map-leaflet', {center: [10.0, 10.0], zoom: 16} );
map.options.maxZoom = 22;
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © Mapbox',
maxZoom: 22
}).addTo(map);
// add location search box
new L.Control.GeoSearch({
provider: new L.GeoSearch.Provider.OpenStreetMap(),
showMarker: false,
position: 'topright',
retainZoomLevel: false
}).addTo(map);
Demo site here
Your leaflet-map init code is being included twice, once inside <script src="/assets/leaflet_map-8e1007491534b1245e2ccc867a845d14e60679a58ca180c5e9cbef68530aa571.js"></script>and once from application.js. Drop the leaflet-map asset reference underneath your map div to begin working through these problems.

Image src doesn't look like it's going through asset pipeline

I'm going through the Rails Tutorial by Michael Hartl. In one of the chapters he is explaining about the asset pipeline, and that if I enter the link_to helper code with the image_tag like so:
<%= link_to image_tag("rails.png", alt: "Rails logo"), 'http://rubyonrails.org/' %>
It should see produce something similar to this:
<img alt="Rails logo" src="/assets/rails-9308b8f92fea4c19a3a0d8385b494526.png" />
but in my browser I see the actual image directory
<img alt="Rails logo" src="/assets/rails.png" />
Note the page renders fine in my browser, but I'm wondering why the code doesn't look like it does in the book.
Because you're probably viewing the page in the development environment. The hashed version of the image is created when the assets are compiled. This typically happens when you deploy your app to production.

Cached app issue - deployed with capistrano

I have deployed my app for first time. Cap deploy was successful. However I was getting an error on a specific code segment. So I decided to delete this code segment and get the website up and running first before fixing this error.
I run cap deploy again but I am still getting the same error on the code segment that I removed from the source file (I see the error by tailing production.log). It was not suppose to be there. I searched around on the net and found that the problem is that there is a cached version of the app. I found out that a solution would be to delete the cache-copy folder in /shared folder.
I restarted my services (nginx, unicorn) and open the site again and I still get the same error on the code segment I removed.
I checked my new current folder, cached-copy folder and last release folder and all don't have the code segment that produces the error.
It's really odd to me. Any clue whats going on?
Thanks!
Code segment that produces the error:
ActionView::Template::Error (undefined method `stripe' for #<Rails::Application::Configuration:0x00000002a85578>):
20: <meta name="viewport" content="width=device-width">
21: <%= javascript_include_tag 'application' %>
22: <%= javascript_include_tag "https://js.stripe.com/v1/", type: 'text/javascript' %>
23: <%= javascript_tag "Stripe.publishableKey = '#{Rails.configuration.stripe[:publishable_key]}';", type: 'text/javascript' %>
24: <%= csrf_meta_tag %>
25: <%= stylesheet_link_tag "application", :media => "all" %>
26: <script type="text/javascript" src="//use.typekit.net/xoh2pss.js"></script>
app/views/layouts/application.html.erb:23:in `_app_views_layouts_application_html_erb__486989174473553269_34754060'
The line 23 was removed but its still shown as generating the error.
# config/initializers/stripe.rb
Stripe::API_KEY = 'asd8df9sadf766'
# application.html.erb
<%= javascript_tag do -%>
Stripe.publishableKey = <%= Stripe::API_KEY %>;
<% end -%>
Are your assets precompiled and the old all.js (or similar) being redeployed without this change present? If the routine invoked by the javascript portion isn't present that could produce an error -- that might be the stripe method you're trying to invoke. If they were manually precompiled and you made the change but didn't re-compile it, the redeploy would simply put the old one back out there.

Following The Rails getting Started Guide and getting a ActionController::InvalidAuthenticityToken

I'm following the rails getting started guide here:
http://guides.rubyonrails.org/getting_started.html
I'm on the step 'Creating posts', where I setup my new view to submit a post to my controller.
When I click the submit button in the view I get the error ActionController::InvalidAuthenticityToken
I was able to get past the error by commenting out this line in the ApplicationController
protect_from_forgery with: :exception
However I'm not sure if I should be doing that. Is that fine or should I dig deeper into the problem? What does that line do?
Content of:
/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Budget</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
No, you shouldn't be commenting that line in your ApplicationController. It is meant for the security of your application at production level.
From the docs: Turn on request forgery protection. Bear in mind that only non-GET, HTML/JavaScript requests are checked.
protect_from_forgery is a feature in Rails that protects against Cross-site Request Forgery (CSRF) attacks. This feature makes all generated forms have a hidden id field. This id field must match the stored id or the form submission is not accepted. This prevents malicious forms on other sites or forms inserted with XSS from submitting to the Rails application. Shamelessly copied from here.
And, in last but not the least. Here is the link explaining why Cross-Site Request Forgery (CSRF) should be taken seriously, and why it is important.

Rails upload file to ftp server

I'm on Rails 2.3.5 and Ruby 1.8.6 and trying to figure out how to let a user upload a file to a FTP server on a different machine via my Rails app. Also my Rails app will be hosted on Heroku which doesn't facilitate the writing of files to the local filesystem.
index.html.erb
<% form_tag '/ftp/upload', :method => :post, :multipart => true do %>
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
<%= submit_tag 'Upload' %>
<% end %>
ftp_controller.rb
require 'net/ftp'
class FtpController < ApplicationController
def upload
file = params[:file]
ftp = Net::FTP.new('remote-ftp-server')
ftp.login(user = "***", passwd = "***")
ftp.putbinaryfile(file.read, File.basename(file.original_filename))
ftp.quit()
end
def index
end
end
Currently I'm just trying to get the Rails app to work on my Windows laptop. With the above code, I'm getting this error
Errno::ENOENT in FtpController#upload
No such file or directory -.... followed by a dump of the file contents
I'm trying to upload a CSV file if that makes any difference. Anyone knows what's going on?
After much research and head banging, I ended up reading the source code for putbinaryfile method to figure out a workaround for the limitation of putbinaryfile. Here's the working code, replace this line
ftp.putbinaryfile(file.read, File.basename(file.original_filename))
with
ftp.storbinary("STOR " + file.original_filename, StringIO.new(file.read), Net::FTP::DEFAULT_BLOCKSIZE)
And in case you are wondering, STOR is a raw FTP command, yeah it came to that. I'm rather surprised this scenario isn't more easily handled by Ruby standard libraries, it certainly wasn't obvious what needed to be done.
And if your app is on Heroku, add this line
ftp.passive = true
Heroku's firewall setup does not allow for FTP active mode, also make sure that your FTP server supports passive mode.
Seems to me that ftp.putbinaryfile just wants the path and name of the file as the first parameter.

Resources