rails assets path - ruby-on-rails-3

I have in
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/application.js" type="text/javascript"></script>
but when I go to http://localhost:3000/assets/application.css
Routing Error
No route matches [GET] "/assets/application.css"
P.S. Rails 3.1.0.rc4, ruby 1.8.7

Seems Sprockets / Rails 3.1 were acting up for me w/ ruby 1.9.2-p180 ... updating to Ruby-1.9.2-p290 seemed to stop the issue.
Maybe not related to your issue... but useful for anyone else having that issue using those versions of Rails & Ruby.

I found that I had something similar going on after updating to Rails 3.1 this evening. I was working on a project that didn't use ActiveRecord, so I had a modified my application.rb to exclude it. The line that usually reads require 'rails/all' to only include the parts I needed, like this:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
This list has changed in Rails 3.1 to include Sprockets, the core component to making the asset pipeline work. I got the asset pipeline serving the serving content as expected by adding this line to the bottom of the list:
require "sprockets/railtie"
After restarting, /assets/application.js and other assets began working as expected.
Note: if you have a custom setup like this, be sure to open the railties gem and look at the contents of lib/rails/all.rb which may have changed (as in this case).

Your scripts and styles will be loaded from the public folder. Drop the assets folder under public and you should be good to go.

In the application layout file, if you have
<%= stylesheet_link_tag "/assets/application" %>
which gives
No route matches [GET] "/assets/application.css"
TRY changing it to
<%= stylesheet_link_tag "application" %>

I had to add the following line in application.rb:
config.assets.enabled = true
in bottom of class Application < Rails::Application

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

Rails asset pipeline - image_path helper only working in development

I have a problem with asset preocompilation in Rails (3.2.7).
I am including an favicon like this:
<link rel="icon" type="image/png" href="<%= image_path("favicon.png") %>" />
On development mode I set config.assets.compile = true. There everything works fine, the rendered HTML looks like this:
<link rel="icon" type="image/png" href="/assets/favicon.png" />
But on production, where I set config.assets.compile = false, I get the error
Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Home#index
...
favicon.png isn't precompiled
I already ran rake assets:precompile and i can clearly see, that the asset is available under public/assets/favicon.png.
I know, that i could set config.assets.compile = true in production, but i don´t want to do that (because of performance reasons).
Has anyone an idea, why my rails app is not able to resolve the correct path to the asset in production? Thanks!
Update:
maybe also useful to know: It happens not only for images, also for other assets.
For example <%= stylesheet_link_tag "screen", :media => "all" %> also produces the error screen.css isn't precompiled when config.assets.compile is set to false.
You must tell Rails which assets are to be precompiled. You do this in config/application.rb or config/environments/production.rb with the config.assets.precompile config key.
Rails starts with a default list of assets to precompile, including ["application.js", "application.css"], but if you want your own assets to be precompiled too, you have to add them to the list.
For example:
# config/application.rb
module MyApp
class Application < Rails::Application
# other config ...
config.assets.precompile += ["screen.css", "*.png"]
end
end
Ok, after a couple of tries I figured out, how to fix this. Nevertheless it´s a little bit strange and does not satisfy me completely. It only worked for me, when I set digest to true and provide the path to the manifest:
config.assets.compile = false
config.assets.digest = true
config.assets.manifest = Rails.root.join("public/assets")
It would be interesting to know, what´s behind this "logic".

How do I get jQuery Mobile precompiled for Rails 3 production?

I'm trying to deploy a Rails 3 app which includes jQuery Mobile. It works fine in development, and I understand that it needs to precompile the JS and CSS for production. I'm getting the following error:
Started GET "/orders/mobile" for 127.0.0.1 at Wed Jun 06 14:22:40 -0400 2012
Processing by OrdersController#mobile as HTML
Rendered orders/mobile.html.erb within layouts/mobile (642.9ms)
Completed 500 Internal Server Error in 1122ms
ActionView::Template::Error (jquery.mobile isn't precompiled):
5: <head>
6: <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7: <title>Company Orders</title>
8: <%= stylesheet_link_tag 'jquery.mobile' %>
9: <%= stylesheet_link_tag 'jquery.mobile.structure' %>
10: <%= stylesheet_link_tag 'jquery.mobile.theme' %>
11: <%= stylesheet_link_tag 'http://fonts.googleapis.com/css?family=Ubuntu' %>
app/views/layouts/mobile.html.erb:8:in `_app_views_layouts_mobile_html_erb__605278794_69818059003840'
app/controllers/orders_controller.rb:330:in `mobile'
app/controllers/orders_controller.rb:329:in `mobile'
I've read the usual stuff, but there are so many things wrong here, I hardly know where to start:
I had the JQM stuff in app/assets, but have since moved them to vendor/assets. They get seen by the precompiler -- I know because it will complain about them on various tries -- but they never seem to get precompiled (in either location).
I've tried *= require_tree ../../../vendor/assets/stylesheets in application.css. I don't really want it included in every page hit, so I'd like to include it in the layout, but I'm just trying to get it precompiled somehow.
I've tried *= require jquery.mobile[[.css].erb] in application.css.
I've tried config.assets.precompile += %w( [[./]vendor/assets/stylesheets/]jquery.mobile.* ) in config/environments/production.rb.
As a last-ditch effort, I've removed the ".erb" from jquery.mobile.css.erb, and removed the <% asset_data_uri %> tags to see if it would compile. It passes the rake assets:precompile command, but still gives me the same error.
I don't want to turn off precompiling for JQM; I want it to work. (I really need to speed this page up.) However, I can't find any guide on how to get JQM elegantly inserted into a Rails 3 app (with precompiling), and I've exhausted every avenue I can think of in trial and error. Surely someone has done this and knows the right way to go about it.
My fundamental problem here is that I need to have some assets precompiled for production, but NOT included in every page hit. Here's what I've figured out:
If I didn't mind JQM being served with every page, I could either put the files in app/assets and let require_tree . do its magic, or leave those files in vendor/assets, and require them individually in application.js. If I want to put them in vendor/assets and NOT specifically require them in application.js, I can use config.assets.paths << "#{Rails.root}/vendor/assets" in production.rb, and (apparently) require_tree in application.js will process this tree as well. This still rolls them up into the compiled application JS "ball," but leaves them distinct on the filesystem.
The proper way to incorporate jQuery Mobile for specific pages -- and keep its files separated in vendor/assets -- seems to be to use both a config.assets.paths << "#{Rails.root}/vendor/assets" directive and a config.assets.precompile += %w( jquery.mobile.* ) directive. This will get the JQM files precompiled, but NOT rolled up into application-(hash).js and application-(hash).css. (They'll be individual files in public/assets.) Then you can do specific javascript_include_tag's and stylesheet_link_tag's in a layout specially for mobile views.
Big finish: I had upgraded to Rails 3.2.5 because I saw some security report on 3.2.3. I rolled back to 3.2.3 and -- incorporating what I learned above -- I was able to finally get it working. I also tried 3.2.4. Apparently there's a regression in > 3.2.3 that interferes with precompiling.

rails 3 rails couldn't find file 'twitter/bootstrap' pt2

This is my second post regarding this issue as my webrick server spits out this message. I've had this before and twiddled with the application.css file to get it to work. My rails is 3.1.3 on ruby 1.9.3. The response from rails is couldn't find file 'twitter/bootstrap'
"all" %>
Researching on Google I've followed seyhunak's responses on this issue. Here is my line in the Gemfile, on its own line and not in a group.
gem 'twitter-bootstrap-rails', :git => 'http://github.com/seyhunak/twitter-bootstrap-rails.git'
Here is what's in my application.css.scss:
*= require_self
*= require bootstrap_and_overrides
*= require_tree .
Here's what's in my application.js
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require require_tree .
I have done the following commands:
bundle update
rails g bootstrap:install
rails g bootstrap:layout application fixed
touch bootstrap_and_overrides.css.less
rails s
After restarting the server, I still get that error. I'm unable to get past this.
I think I've exhausted all Stack and Google can suggest. What am I overlooking? thanx, sam
After beating myself up over this, I got some Rails devs to look at this problem. My understanding is that Twitter/Bootstrap needs files to be in the vendor directory. The gem would then need to be removed, commented out, so they would not conflict. My app now shows pages that look like the project's homepage. I'm unsure if I've lost the ability to use less to change things. This is my inexperience showing. Thanks for looking.
Moving gem "twitter-bootstrap-rails" out of group :assets
And doing a bundle install worked for me.
Here is some more information
https://github.com/seyhunak/twitter-bootstrap-rails/issues/91
I have similar problem,
I use application.html.haml instead of application.html.erb
using this link:
HTML to Haml
Code is as:
%html
%head
%title Task
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= javascript_include_tag "application", "data-turbolinks-track" => true
= csrf_meta_tags
%body
= yield
You Must install Haml gem first:
install haml gem rails

Rails 3.1 asset pipeline not seeing files

We're upgrading our app from Rails 2 to Rails 3.1, and I'm having trouble with the asset pipeline.
I've got the following in my config/application.rb:
if defined?(Bundler)
Bundler.require *Rails.groups(:assets => %w(development test))
# Bundler.require(:default, :assets, Rails.env)
end
# Enable the asset pipeline
config.assets.enabled = true
# config.assets.prefix = "/assets"
config.assets.paths << "#{Rails.root}/public/images"
config.assets.paths << "#{Rails.root}/public/stylesheets"
config.assets.paths << "#{Rails.root}/public/javascripts"
config.assets.version = '1.0'
And then this in development.rb
# Do not compress assets
config.assets.compress = false
config.assets.debug = true
I know this isn't the desired behavior for the pipeline, but we are doing it this way to make sure that when we merge the upgrade back into our master branch, all the old files are accounted for properly.
I then have the following file, "all.css," in my public/stylesheets directory:
/*
*= require ezform
*= require jquery-ui-1.8.9.custom
*= require thickbox
*= require yui-upload
*= require styles
*/
I am calling it from within my layouts/application.html.erb file like so:
<%= stylesheet_link_tag "all" %>
Loading things up in a browser, however, I get no styles (or javascript, for that matter). Firebug and Chrome tell me that the .css and .js files are being looked for in "/assets" - it's like the pipeline isn't searching through everything and bundling it like it should.
The error looks like this:
GET http://localhost:3000/assets/jquery-dependent.js 500 (Internal Server Error)
If I move "all.css" into /app/assets, it still won't find it. Moving it into /assets stops the error, but the stylesheet doesn't compile and I still don't get any styles in my browser.
There's also a slew of errors that look like this in my log:
Started GET "/assets/defaults.js?body=1" for 127.0.0.1 at 2011-12-22 14:35:36 -0600
[2011-12-22 14:35:36] ERROR NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
/Users/kevin/.rvm/gems/ruby-1.9.2-p0#media3/gems/rack-1.3.5/lib/rack/handler/webrick.rb:71:in `service'
/Users/kevin/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/kevin/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/kevin/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
cache: [GET /assets/defaults.js?body=1] miss, store
Served asset /defaults.js - 200 OK (1ms)
What am I missing?
sigh It was memcached. I tured it on (memcached -d) and now all my assets are appearing. I'm not sure why, so I'd love some explanation. Otherwise, it's working.
There are a while bunch of settings that need to be added into the development and application config files for the pipeline to work correctly.
Check out the last section of the pipeline guide for details of these.
Once you've done that I suggest that you change the manifest names to application.css and application.js as these are the default names and you'll run into fewer problems starting with those. Edit your question if it still does not work and I'll see if I can help after that.