RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I think it is the same, edit apache's port for the virtual host if you want :3000, port :80 is just the main port so use http://192.168.0.135 without a port and it should go if it is not blocked by a firewall.

Avatar

Passenger does not work for Windows, Phusion says it will be unefficient and give a bad name to both them and rails. There are no plans to port to Windows, so people like me, are stuck with a Mongrel cluster. But, if anyone needs to know where Windows host files are, they are: C:\Windows\System32\drivers\etc and the files you edit are hosts and imhosts.sam. Linux supports it, so I would consider a dual-boot but the new version of Ubuntu seems to always screw up my Windows partition.

Avatar

/usr/bin/rake? ugly enough
replace it with which rake

Avatar

Assets Pipeline has nothing to do with including the scripts.
you can use config.assets.precompile to configure which assets will be generated. But that's about it.

If you want to include the script into /admin pages just use normal javascript_include_tag('admin/your-file.js').

Avatar

For me in the development it is not that slow.
Although during JS unit testing it is. But it is solved by guard-rails-assets.

Avatar

1) You have to reference those files appropriately as someFil.js and subfolder/someFile.js. There is no collision.

2) Every file is only included once - the first time it is required implicitly or explicitly.

Avatar

Is it possible for CarrierWave to generate URL for downloading files from S3 which will expire in certain of time? I'm having difficulty in preventing users from downloading files if some conditions met.

Avatar

I'll add to that by saying you have never been able to write to the public folder on heroku anyway. For up-loaders like carrierwave you need to re-define in your model where the tmp upload goes before going off to S3

Such as:

def cache_dir
"#{Rails.root}/tmp/uploads"
end

Avatar

+1 I just jumped into the comments to mention this issue also :) Glad someone else has already noticed.

Avatar

Thanks for the great intro! I particularly appreciated the part about how to exclude "admin" javascript from your "public" application. However, it seemed like you only answered half the question.

You showed how to always prevent the inclusion of files in /app/assets/javascripts/admin from the compiled application.js file for all controllers/views.

However, you didn't show how you would go about including the /admin js files when viewing an admin view. How would you do that?

Avatar

Wouldn't it be possible to use ruby threads for the use case shown in the cast?

Avatar

Hi! i'm having the exact same issue as @Kevin Whitaker. But also i don't get the logic of this fix, i dont have any child - parent relationship, i only have my avatar attached to a user and thats it. Maybe i'm not getting the logic right, so if someone could solve that 406 problem (blank HTML screen) please help me.

Thanks in advance.

Avatar

anyone know how to get this working with Kaminari?

Avatar

Any tricks for speeding up asset serving in development?

When I have been playing with a 3.1 branch of our main rails app at work I experience terribly slow performance. I gather this is because Rails is serving every request for every asset (as seen in the log) instead of Unicorn doing it.

I ask since the performance I see is so bad I can't imagine any developer coping with it... hence I must be missing some crucial bit of information.

I guess the reason assets don't work like Compass (which generates static files in public) is that I can make use of a full request object with session and all in my assets or something?

Avatar

Oh works now, had to restart the server after putting something in vendor. It's probably the same with the lib folder.

Avatar

anyone know how to make this work with 'rails', '3.1.0.rc4'?

Avatar

Yes, by a mile.
Solr does not depend on a DB, unlike thinkng sphinx

Avatar

Fantastic! This came just as I was scratching my head about a few things. I do have a couple of questions:

1) What happens in the case of duplicate files? For example, I have someFile.js in my app/assets/javascripts folder, and another one in app/assets/javascripts/subfolder -- are they both included, or just the first one on the path order?

2) What happens if I list a file in the manifest that is also included in the require_tree . list? For example, I have a JS file that I need to ensure it loads before anything else. I list it manually, right above the require_tree line; will that file then be included twice?

Avatar

oops, thx, that works! ;-)

However vendor/assets and lib/assets are not included...

I tried putting something in vendor assets, but it didn't work.

Avatar

scss has a asset-url method you can call.

ruby
    background: asset-url('body-bg.gif', image);
Avatar

Just use
Rails.application.config.assets.paths instead of
Rails.application.config.assets.path

Avatar

Rails.application.config.assets.path returns nil for me...

Loading development environment (Rails 3.1.0.rc5)
ruby-1.9.2-p0 > Rails.application.config.assets.path
=> nil

Avatar

As always, great railscast. But what'd I'd really like to get peoples' opinion on is how they have/will use this asset pipeline to better merge the work of designers/html coders and web developers (as I'm assuming most of you guys are).

I used to set up a "mockups" folder in "public" so the mockups and the real app pulled from the same javascript, images, and stylesheets. This really isn't possible anymore, so I've changed to teaching the designer some basic rails so he can write things like <%= asset_path 'background.png' %> in application.css.erb. Anyone have good suggestions on a better setup?

Thanks very much!

Avatar

If the coffeescript gem is in your gemfile, it should compile automatically yes.

You also need a js compiler in your gemfile, rubyracer seems to be a popular choice. At least that was an issue with rc4

Avatar

Is there a technique for using the assets pipeline on Heroku? Since we can't write to the public directory, I'm wondering if there is a configuration option to use tmp?

Avatar

Hi Ryan,
as always a terrific episode of railscasts.
I'm currenty working on a little project, where I use Coffeescript to generate the necessary Javascript.
Do you - or does anyone reading this - know, if coffescript is automatically compiled when application.js is requested? Or do I need to compile it myself and put the resulting .js-file in the assets-folder?
What's best practice for that scenario?

Thank you for you insights.
Greetings, keyJ

Avatar

This is very useful. Can you please get into more details of the asset pipeline? You have a knack of getting to the point and getting it across.
Thanks.
Bharat

Avatar

I put images specific for my app in the app/assets/images directory. Depending on the complexity of the application you may want to store some images in the lib/assets or vendor/assets directories. It actually doesn't matter but convention says:

app/assets - assets specific to your application (custom images, custom javascript, etc.)
lib/assets - assets that you wrote but are may be shared across applications (shared icons, javascript, base stylesheet)
vendor/assets - assets not specific to you app or written by you (Jquery plugins, CSS frameworks, etc)

Avatar

Nice episode! Would be interesting to see more about Tilt.

Avatar

Great cast! Could you elaborate on where would you put images and why, in app/assets or public/ directory?

Avatar

Nice job as always. Can't wait to see your screencast explaining Tilt in more detail.

Avatar

It doens't have an index action; but it shouldn't have one. Not conceptually, and not according to Ryan's code. Even when I copy/paste his code, it gets the error. (Only in testing, not when I click it manually on the web). Maybe it's a bug in Rspec/Capybara?

Avatar

If I'm using Nifty Authentication to handle Logins, Registrations, Logouts (etc) does anyone know how to make this work with jqTouch?

As it seems the AJAX form submitting is breaking the application, and when I click log in, it fades to a black background?

Avatar

it will be nice to have an update for this Railscast with Rails 3.1 and large volume of records.

Avatar

Thanks Ryan.

@Olivier

1) Create new Helper

class Buildr < ActionView::Helpers::FormBuilder

def label(method, text = nil, options = {}, &block)
str = @template.label(@object_name, method, text, objectify_options(options), &block)
str = str + "*" if object.class.validators_on(method).map(&:class).include? ActiveModel::Validations::PresenceValidator
return str
end

end

2) In form_for call :builder => Buildr

Avatar

Does this lesson work in rails 3 as is, or are there some modifications to do? I can't seem to make my included javascript files to work

Because when I look in my document head I have

the application.js showing

But when I open the firebug script console the application.js isn't available or showing

Avatar

Hi I'm relatively new to rails and havvn't used a plugin before so just a little lost at the beginning.
I'm using Ruby on Rails and can't follow where does the bash code goes.
Help please

Avatar

Found - I needed to add require 'sprockets/railtie' to dummy application.rb (when not reqruing the whole rails).

Then assets can be accessed via Rails.application.assets.

Avatar

I wonder how can I access the assets data in a test?

I tried Dummy::Application.config.assets['application.js'] but it always returns nil for all the assets.

Probably I should initialise it somehow...

Avatar

I want to use Faye on production server. how can I start faye server automatically or as a daemon process.

Because when I start faye server using SSH it shutdown as i close ssh connection. Please guide me its really urgent.

Avatar

Exactly... I feel like these tutorials are somewhat incomplete seeing as how the most difficult part of the process is left out.

Not to sound ungrateful, but I would really love to see a bigger emphasis on Rails deployment in general, because IMHO it is by far Rails' biggest drawback.

Not everyone has the money for Heroku & Websolr!

Avatar

Thank you for the great cast! Is there a way to share the layout between main app and engine app?

pado

Avatar

Roy - did you restart your server after creating the new Mime type?

Avatar

I ran into problems when trying to use more than one token input field on one page. Here is how I changed the code to make it work:

$(function () {
//$('#item_tag_tokens').tokenInput('/tags.json', {
$(".token_input").tokenInput('/tags.json', {
crossDomain: false,
//prePopulate: $('#postyle_tag_tokens').data('pre'),
prePopulate: $(this).data('pre'),
tokenValue: 'name',
theme: 'facebook'
});
});

and in the view:

<%= text_field_tag 'postyle_tag_tokens','', "data-pre" => postyle.tags.map(&:attributes).to_json, :class => "token_input" %>

Avatar

Great screencast!
I noticed it's really easy to implement this with the virtual attribute tagging tutorial (with various taggings with tags). Like so:

ruby
text :taggings do
        taggings.map{ |tagging| tagging.tag.name }
end
Avatar

Really useful screencast!

One important note is that you have to filter your logs or else you are still storing all your users credit card information in plaintext.

I would add a "filter_parameters" line to this screencast so that everyone knows about this potential security pitfall.

Avatar

Thanks Ryan.
Just curious how would you test the search and/or fake it for other tests?