Rails 3.1.1 getting broken image on image assets - ruby-on-rails-3.1

I'm missing something with the asset pipeline functionality. Things work fine in development and I move to run in production environment and all hell breaks loose!
So I have an image in app/assets/images named "logo.png"
I precompile (RAILS_ENV=production rake assets:precompile) and I can verify that in public/assets there now exists both a logo.png and logo-5fa60e416f495e562c56a5087fe696dc.png
I then run in production rails s thin -e production
But I get a broken image
GET http://localhost:3000/assets/logo-5fa60e416f495e562c56a5087fe696dc.png 404 (Not Found)
What is going on?
I also notice that when I switch to production I also starting getting javascript errors like "JQuery is not found" for jquery tools EVEN THOUGH no problems in development AND I specify in my application.js that jquery should be loaded first before any other javascript files.

The solution is simple (finding it was not).
Simply update your config/production.rb to reflect the following:
config.serve_static_assets = true
Though this is set to false by default, only when you set it to true will you be able to properly run your rails 3.1.x application locally (via webbrick or in my case thin) in the production environment. You'll probably want to set it back to false before you deploy to your actual production server.

Related

Laravel, codesleeve asset-pipeline not working

I am trying to get codesleeve asset-pipeline to work on my site. After wasting a full day, I finally got it working yesterday on my local development server. I have now uploaded the project to see if everything works on the live server. Guess what- it doesn't. I am guessing the problem is to do with the environment setting, ie:
$env = $app->detectEnvironment(array(
'local' => array('your-machine-name'),
));
So, I have changed that to my machine name when on my local server, which, as I say, I have working. So on the live server, what should this be? "localhost"? I have tried adding another array key for 'live' with my live server's IP address and domain name, as per this answer: http://tinyurl.com/pg6hwum Nothing works.
Also, according to the tutorials I am following:
When you are in the local environment, you will notice that all of your asset files will be included individually ... Once you are in the production environment, all of your assets will be concatenated into one.
That doesn't seem to be the case for me either, as pipeline seems to be half-working, ie. it is concatenating my js and css files, but somehow messing them up.
I'd really appreciate some pointers, as I have wasted a colossal amount of time with this thing now. Thanks.
First set your environment settings on your bootstrap/start.php file into something like this:
$env = $app->detectEnvironment(array(
'local' => array('http://localhost', '*.local', 'http://local.sitename'),
'development' => array('http://dev.sitename.com'),
'production' => array('http://www.sitename.com', 'http://sitename.com'),
));
Second you need to check what enviroment does your laravel is running to check if your environment setup is working properly. By doing this:
App::environment()
So by doing that..you should have idea now what is causing the problem on codesleeve asset-pipeline.
note: see the documentation about environment for more details here
Asset pipeline will out of the box behave differently when on production compared to another environment (like local). I have put in a proposal to document some of these pain points and troubleshooting for new comers.
If you are using the most current version then there are two big behavior changes when switching environments:
caching - production assets are cached. This means once you load a page you will be stuck with those assets until you do
$> php artisan assets:clean
This is why you should set your environment to local when developing.
minification - assets are minified on production only. This can cause issues though because the minifiers sometimes break on code. There are some workarounds you can do to fix this. Something I normally do when having issues with a certain file (say Twitter Bootstrap) is use the .min.css extension which is then skipped by the minifier and written out as-is.
Note that this behavior is out of the box and can easily be modified by editing your configuration.
$> php artisan config:publish codesleeve/asset-pipeline
Then edit your file app/config/packages/codesleeve/asset-pipeline/config.php. For more information on these options visit the documentation here.
Hope this helps.

Rails 4 - ActiveAdmin - Redactor - Heroku -- has no method 'redactor' console error

I've been trying to tackle this all morning. Locally, this setup works just fine. I am not using the redactor-rails gem because this is a rails 4 app and it doesn't support it. I have moved the redactor.css and redactor.js calls from the active_admin initializer file to be required in my actice_admin.js.coffee and active_admin.css files because on Heroku, it could not find the path. Now, when firing up the page that should be displaying redactor, I get has no method 'redactor' in the console.
I'm at a loss. I've read up on precompiling assets and the how the pipeline works, but it doesn't seem like I'm doing anything out of the ordinary here.
So how can I deploy to Heroku without this error?
Ok, so I seemed to have disregarded an important task. After thinking about precompilation a bit more I remembered I had been told to run
RAILS_ENV=production bundle exec rake assets:precompile
before. After running that again, it precompiled the active_admin.css and active_admin.js.coffee files which had the new require's in it. Viola!
Hope this helps someone else in the future.

Ruby on Rails "hot deploy" not working

I don't know if the correct title for this is hot deploy.
I am modifying controllers and views and I have to restart the server to view those changes.
Why? I think that it didn't worked like this.
I am using webrick server. Environment is development.
I am using Rails 3.1
Go to your config/environments/development.rb and make sure you have:
Railsapp::Application.configure do
config.cache_classes = false
# ...
end
In mine, it comes with this explanatory comment:
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false

Paperclip routing error in Rails 3.1: only applies to new images

I am having essentially the same problem as this questioner: Paperclip routing error. The server gives a 404 for a static file (which is present!), so Rails gets called and then there is a routing error.
My Rails 3.1 app uses paperclip to attach files, but when serving files in production the following error is logged by Rails:
Started GET "/system/images/9/thumb/Large%20image.jpg?1320149338" for [...]
ActionController::RoutingError (No route matches [GET] "/system/images/9/thumb/Large%20image.jpg"):
Corresponding to an NGINX access log entry:
"GET /system/images/9/thumb/Large%20image.jpg HTTP/1.1" 404 728 "-" [...]
So, a few observations:
This didn't happen in the past, and past images with almost identical URLs still work, e.g.: /system/images/1/thumb/StoneSkimming%20Demyhb%20Art%203.jpg is served correctly.
The image files were uploaded, and converted by ImageMagick, correctly - I have verified all the expected files are in the right place, and are valid JPEGs with the right image in them.
Permissions for the files are all as expected, and match the files that still work.
The entries in the database for the relevant model look correct.
Adding/removing the query string, or putting the URL directly into the browser gives the same result as the embedded <img> tag: the image is not found.
Can anyone point me in the right direction here? I am having trouble working out how to test/debug this because the error is not universal across the app.
I am using Capistrano to deploy, so /public/system is a symlink to the shared directory. My instinct is that this is not the problem though, as older images in the same location work just fine.
I managed to solve problems with paperclip when deploying with capistrano to production by adding to capistrano deploy config:
namespace :deploy do
desc "build missing paperclip styles"
task :build_missing_paperclip_styles, :roles => :app do
run "cd #{current_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
end
end
after("deploy:update_code", "deploy:build_missing_paperclip_styles")
Probably there is better solution.

Rails 3.1.rc1 Image Tags

I'm trying to implement Rails 3.1 in my project, but none of my image_tags are working.
I really need nested has_many :through, which is why I choose to upgrade.
I know that images/javascripts/stylesheets should now go into app/assets/
However due to complications with my theme, I would like to keep them in public/
How would I go about doing so?
The image_tag, javascript_include_tag, and stylesheet_tag are looking only in the assets directory.
Console Output Example
Started GET "/assets/icons/small/white/Alarm%20Bell.png" for 127.0.0.1 at 2011-05-27 11:04:48 -0400
Served asset /icons/small/white/Alarm%20Bell.png - 404 Not Found (0.00488s) (pid 1656)
That file is really located in "public/images/icons/small/white/Alarm Bell.png"
Thanks
Got it.
In application.rb, at the bottom, set the asset pipeline to false.

Resources