Assets url Rails 4.1 and Heroku - ruby-on-rails

Something always seems to go wrong when deploying an app to heroku, today its my images not being rendered.
I ran
rake assets:precompile RAILS_ENV=produciton
committed changes and pushed to heroku
In my view for example i have
<%= image_tag('/assets/logo.png') %>
but the image is not being rendered because its looking for logo.png as opposed to
logo-321321327454547676576586876.png
which was generated when running the precompile.. I think im missing an option in my production.rb file somwhere
Rails.application.configure do
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :info
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
end
How do i get the app to look for the generated image url created when precompiling? something simple Im guessing
I am going to use an S3 bucket to serve these in the future but am interested in getting it working with Heroku on its own
Thanks

After reading the api docs in more detail and taking Baloo's advice the answer is really simple, just a syntax issue with my path declarations
If your image is under assets just use
<%= image_tag('image.ext') %>
and if its in a sub directory then use
<%= image_tag('sub-dir/image.ext') %>
Hope this helps someone else

Related

How can I speed up Rails development server?

I am working on Rails 4.2 & Angular 1.4.8 as the front-end. This is my development.rb file:
Rails.application.configure do
config.cache_classes = false
config.eager_load = false
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = true
config.assets.raise_runtime_errors = true
config.action_mailer.delivery_method = :letter_opener_web
config.action_mailer.default_url_options = {host: "localhost:3000"}
config.consider_all_requests_local = false
end
I know that by disabling assets.debug I can really speed this up, but I need to have my assets refreshed when I refresh the page. I am doing full stack so I can't precompile assets all the time.
I recently started using guard with guard-rails - do I have any chance ?
I suspect that compiling all assets every time after file changed.
if you require angularjs libs and other libs in to application.js try to create a separate file for libraries and application
See also: Rails 3.1 is very slow in development-mode because of assets, what to do?

Rails HTML not changing when I push to production

I have a rails app hosted on ubuntu and served with Apache2 and Passenger. I am having issues getting changes in one of my views to actually reflect in the server. I am not talking about css but the generated html is not changing. I am at a loss for what to do. I am using turbolinks -- though im not talking about css. I have a new.js.erb file that isn't rendering on load and I have changes to a partial view that aren't rendering on restart either. All of this works in development.
production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = true
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
restarting the server with passenger-config restart-app
Edit 1
edit.js.erb that will not load
$("#deal_<%= escape_javascript #deal.id.to_s %>").fadeOut("slow", function() {
var div = $("<%= escape_javascript render partial: 'edit_deal', locals: {deal: #deal} %>").hide();
div.find('.save_deal_link').first().click(function(event) {
$(this).closest('form').submit();
event.preventDefault();
// Prevent link from following its href
});
$("#deal_<%= escape_javascript #deal.id.to_s %>").replaceWith(div);
div.fadeIn("slow");
});
When I attempt to access this code directly by following baseurl/accounts/2/deals/1/edit.js I get error 422 which seems to just be a standard CORS error from the log. I think a better clue to the problem is that in a partial _deal.html.erb the changes are not being reflected on the production environment.
Edit 2
I am noticing that the click handler is not firing for the link that makes the ajax request, and the production server appears to be serving the new.js.erb -- it's just changes in the view(.html.erb) are not seemingly getting served no matter how many times i upload the views folder
I changed the location of the partial from /accounts/_deal.html.erb to /application/_deal.html.erb and forgot to remove the old one on production.

Heroku push incomplete

I have a rails app that works perfectly in localhost, but last time I pushed it to Heroku the "About" and "Contact" sections seems like incomplete upload or something, here is the link to the website https://damp-inlet-9409.herokuapp.com, and yes, I re-pushed like 3 times and still the same result
Update:
I think the problem is from development.rb or production.rb, here you got them:
Production.rb
Rails.application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
end
Development.rb
Rails.application.configure do
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true
end
There are several potentialities with your issue.
Firstly, you're getting a rails error, which means that at the base level (IE Heroku database etc), the app should be working okay.
The main problem for many Heroku apps is that they either don't have a database, which would yield the Heroku "Application Error":
For future reference, this error means that you have an issue with how Heroku is running your app (typically that you don't have a db or something).
Since you have a rails error, it means that the problem is likely an undeclared variable, or missing conditional login etc.
--
To fix the issue, you need to check the Heroku logs:
This will give you a specific readout of the problem that's preventing the app from running. You'll then either be better placed to give us the details of said error, or fix the problem yourself.

What is wrong with my heroku asset:compilation?

I am trying to insert an image into a mailer, everything runs locally but heroku doesn't see the image on its server despite pushing and precompiling as I'm ought to do. Code as below;
user_mailer.rb
def welcome_email(user)
#user = user
attachments.inline['my_logo_sml.png'] = File.read('app/assets/images/my_logo_sml.png')
mail(to: #user.email, subject: 'Welcome to Mybiz')
end
welcome_email.html.erb
<td class="six sub-columns header_logo">
<%= image_tag attachments["my_logo_sml.png"].url -%>
</td>
gemfile
gem 'rails_12factor', group: :production
app/assets/images/my_logo_sml.png is clearly present and getting through the pipline locally.
I have run RAILS_ENV=production bundle exec rake assets:precompile a number of times, I have heroku restart'ed a number of times.
Heroku logs:
2015-06-03T22:38:50.824191+00:00 app[web.1]: Errno::ENOENT (No such file or directory # rb_sysopen - app/assets/images/my_logo_sml.png)
I'm at a loss after hours going round and around on this, can anyone help me please?
Update
After hours of looking at this i feel like it's not a syntax issue but an asset pre-compilation issue of some sort, I've copied my config/environments/production.rb code below if anyone can spot anything wrong?:
Rails.application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.compile = true #concerned that this is the right thing to do?
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
config.action_mailer.asset_host = 'http://mysite.co'
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
end
Perhaps I could clean all compiled assets out and start the whole thing again, could anyone advise as to how to do this?
Update
The source of my problem seemed to be confusion between local and slug asset compilation. I ran heroku run rake assets:clobber and this solved all of my problems. Full understanding of what precisely was going on still eludes me, however running the above and having config.assets.compile = true in config/environments/production.rb has everything working just fine and upon git push heroku it shows me clearly that it's running both rake assets:clean and rake assets:precompile just fine. Syntax used in the view was:
welcome_email.html.erb
<%= image_tag('my_logo_sml.png', alt: "My Logo Small")%>
I'm still unsure as to whether having config.assets.compile = true is the correct thing to do despite it seeming to solve the problem, i wonder whether it might end up compiling assets on multiple occasions?
I had similar problem. I think welcome_email.html.erb file can not be .erb file. There is a workaround. You can put image somewhere on the web like Amazon or Image share services and use link as source.
<img src='https://example.com'>
Reading the file from Rails console as suggested by #user123 shows that the paths are correct and the file is there. Trying to reading the file from Heroku run console does not find the file and comes up with the same Errno::ENOENT no such file error Strangely heroku is serving an image in the homepage which i try to load as described above in heroku console and it is unable to find it.

Precompile with compression in development environment

I'd like to run the task
assets:precompile
with compression in development environment.
I've set
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
in development.rb.
But compression with assets:precompile only seems to work in production mode, is that right?
Michael Kastner
For file compress in rails try this following method it will work exactly
My config/environments/development.rb has this, which I interpret the rails guide to mean that assets should be compiled into one file, but not compressed:
`config.assets.compress = false
config.assets.compile = true
config.assets.digest = false
config.assets.debug = false`
Try to add debug: false to your include/link-tags
It work means please
{# in views/layouts/application.html.haml (or .erb, then use <%= %>)}
= stylesheet_link_tag "application", debug: false
= javascript_include_tag "application", debug: false
No need to restart app! I hope you didn't forget to do it after you had changed your development.rb ;-).

Resources