RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Thanks Javi. Good to know.

Avatar

@Dan: no, because of the escape_javascript function.

Avatar

I'm pretty new to rails but does using "raw" open you up to any sort of security concerns? For example if you added a comment that had javascript in it like so: <script language="javascript">alert('hello')</script> would that run because you're using raw?

Avatar

Best table sorter solution I've tried: http://tablesorter.com/docs It's all js works with pages and has tons of options. It's also really easy to get running.

Avatar

so i GUESS i fail to understand why reference in this tutorial aren't LAID out a little better...

example :

first line:
# models/product.rb

instead of:

# app/models/product.rb

perhaps it would be of use to more people with a little more explaining

and nowhere does it say that this tutorial requires additional files...

like where am i supposed to get these??

i guess i simply dont follow the logic of why (obvious) things are missing from the tutorial

Avatar

Hi!
I know this screencast is a bit old, but for those interested in Faker
The Faker gem is a bit outdated now, try the ffaker gem instead.
gem install ffaker
I have found some useful class methods (in Faker Module) in the documentation:
letterify numerify and bothify, which will convert your placeholders in your string in alphabetical characters, numerical or both.

<script src="http://gist.github.com/558734.js?file=gistfile1.rb"></script>

Thus, you can really customize your test data to your needs.

 :)

Avatar

Hi!

Great screencast as usual. Nothing new to me (as I earn my beard by writing jQuery), but I sent it to a friend, which was curios how to do Ajax.

Do you plan to add a Flattr button to your page? Flattr a smart mix between the like button and donating, but not as clumsy as Paypal and it's getting very popular in Europe right now. I would click every single episode!

Regards from Germany,
Johannes

Avatar

Best to display it to the user as a progress bar in my opinion. That way you keep him distracted instead of just displaying a 'please wait' message :)

Avatar

CSS ( Cascading Style Sheets ) Tutorial - Why CSS ? - Introduction - Css Link Properties / Examples - Css List Properties / Examples - Css Layer Properties / Examples - Css TextBox Properties / Examples - Css Font Properties / Examples - Css Text Properties / Examples - Css Cursor Properties / Examples - Css Background Properties / Examples - Css Table Properties / Examples - Css Scroll Bar Properties / Examples - Tools - Units - Parameters - Template Page - Web Design Tutorial

Avatar

Great episode! But I noticed that you are using clas selector in jquery, which is very slow in IE. I think it's better to write at least like this one:
$("input.comment")
Just put the element before the class name and it will improve the performance.

Avatar

Practical and straightforward as usual. Thanks, Ryan.

However, I have never been comfortable with having the server return a big chunk of escaped JavaScript that inserts HTML into a particular page. The problem is that the server has to know the structure of the target page (dom IDs, etc) so the response can only work with that particular page. I prefer the server to return a plain HTML response and the page to act as a client that receives and inserts that HTML properly. That way if we have two differently-structured pages that need to poll for new comments they can use the same URL (controller/action). This makes the server code simpler and more generalized and the JavaScript less fragmented because the page's full behavior is all coded in one place (you don't need index.js.erb).

This is easy to implement by listening for AJAX events, as detailed here:

http://code.alexreisner.com/articles/link-to-remote-with-ujs.html

Just a thought. Thanks again for the great screencast.

Avatar

Thanks so much for this! I wish I had seen this before.. I'm going to go redo my comments area now! By the way, did you experience any issues upgrading to Rails3?

Avatar

I've had a lot of success switching my polling-based notification system to node.js & faye. Database traffic dropped by over 70% for my app, since node could push the data and completely avoid the db. Crazy!

Avatar

Elad, you can use WebSockets: http://en.wikipedia.org/wiki/WebSockets

Avatar

Elad, I'm guessing you actually meant that you want server push and with Rails combined with Nginx there is one way forward with a HTTP push module for Nginx: http://github.com/slact/nginx_http_push_module

Here's an article describing how it could work: http://www.igvita.com/2009/10/21/nginx-comet-low-latency-server-push/

Cheers

Avatar

great one as always..
is there a simple way to make it a pull and not a push, so the server will not get lots of hits.. (you said something about a socket.. is this is the only way?)

it will be nice to have something as Gmail notify only when a new msg arrived.
Thanks

Avatar

it's really nice to have jquery in rails3.

thanks for this Ryan!

Avatar

Pls use this migration for user
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :username
      t.string :email
      t.string :crypted_password
      t.string :password_salt
      t.string :persistence_token
      t.timestamps
    end
  end
  
  def self.down
    drop_table :users
  end
end

else you will get a undefined method 'password_confirmation' for method foo error.....

Avatar

probably something like this is needed:

# app/controllers/users_controller.rb

class UsersController < Devise::RegistrationsController

# GET /resource/sign_in
def new
   super
end

# POST /resource/sign_up
def create
   # add custom logic
   super
end

...

end

Avatar

it would be great to do an update of this episode, so it works nicely together with Devise (see episodes 209,210).

When generating an Model through
Devise, Devise doesn't create a user_controller out of the box.. maybe you could put a note in here, on how to deal with that when trying to implement Beta-Invitations with Devise

Avatar

Ryan.
I am testing this using a constant, the first part of the video with just some strings in the model.

I have one question, once a role is updated, how can i display the role in the view?
I tried <%=h @user.role %>
But no luck, basically I just want to display something like role: admin.
how do i go around this?

Avatar

Hi Ryan,

I hope this comment does not get lost in all the spam that has filled your page :(

I am using pg adapter(postgres) instead of sqlite3.

While playing with the console I found that when i do Article.order("name") i do not get a list ActiveRecord::Relation objects(as shown in the screencast). You spoke about lazy-loading.. this however does not seem to work with the setup that i have. I instead get the entire list of articles (as you would if you did Article.order("name").all).

Has it got anything to do with the postgres adapter?
Is this a bug that needs to be reported?

Thanks,
Shripad

Avatar

@Benoit Daloze

Try calling your method like this:

sortable("some_col", nil) # title will be nil instead of the default value

That's not exactly the same behaviour.

Avatar

Just a little style note:
def sortable(column, title = nil)
  title ||= column.titleize

#=>
def sortable(column, title = column.titleize)

Great episode

Avatar

Other fantastic alternatives include jQuery History which provides nice tracking of hash changes for your website. And jQuery Ajaxy which allows you to have a rich ajax website while still supporting the back and forward buttons and no server side changes.
jQuery History: http://www.balupton.com/projects/jquery-history
jQuery Ajaxy: http://www.balupton.com/projects/jquery-ajaxy

Avatar

Ryan, I know this episode was released a few months ago but you might want to consider releasing an update for it since the current versions of Devise is 1.1.2 and has a few changes. Thanks for the screencasts.

Avatar

The idea of the plugin is to hold modules that are not handled by Authlogic by default. So those who wants an authorization system, would consider building it on top of AuthHelpers, so you will have to do it just once!

Avatar

This tutorial is outdated, plugins and gems no longer working. Don't follow it...

Avatar

@"Confused from Rails 2.3.3":

Rails 3 syntax:
Product.order(sort_column + " " + sort_direction)

"plain old" Rails 2.3 syntax:
Product.all(:order => sort_column + " " + sort_direction)

Avatar

@Dakshata:
I have problems when using VLC and playing the video from the network.
 Downloading it first, then playing the downloaded file works fine however.

Avatar

Just what I needed for my site. Thanks.
But you are still getting some smarter spam an this site. I advice you to make a list of all the new comments so you have a better view over them.

Avatar

Hi,How to fit the large string into single textbox without breaking the word in active reports.

Avatar

I'm trying to use an invitation_observer.rb to send the email. How do I get the signup_url(invitation) method into the after_create method?

Avatar

HI,

I need to know if any nested attributes is blank then how it would work?
e.g -
In my case user have addresses & photos(nested attributes) but not mandatory while adding user if i haven't added photos & submit form then while editing that user it will not shown me photo upload field because photo object is not exist for that user

can you explain me how should it work?

Avatar

Ryan, I always manage to learn something totally useful from every episode! I've done sortable columns a million times, but the cmd + option + a text mate trick is a life saver. Thanks and great as always!

Avatar

Nice episode, very clean and informative.
    
For those interested, there's a gem called handles_sortable_columns which gives you this exact functionality with just 2 lines added to your controller. Very simple, yet configurable and fully documented.

http://github.com/dadooda/handles_sortable_columns

Ryan, thanks for encouraging me to publish the stuff I've been putting off for a few months already. :)

Avatar

Hello Ryan,

The video doesnt work after a certain point.

Is there any problem?

Thanks

Dakshata

Avatar

A hint for anyone stuck trying to override the default model when using the scaffold generator: make a template file in lib/templates/active_record/model/model.rb

Avatar

Hi, great episode! I was just trying to implement this on my site (Rails 2.3.8), but I'm confused. What does the "order hash" described as the Rails 2 alternative to the Rails 3 Product.order(params[:sort]), look like?

Thanks!

Avatar

Also, I would love to see a tutorial on integrating Facebook connect with Devise (since that's my next step!)

I love your tutorials by the way. Your tutorials are one of the reasons why I am switching to Rails. They make it easy for me to learn quickly. Thanks for what you do for the community!

Avatar

To make this clear for everyone: If you are using Rails 3, and you're getting this message: No route matches "/" or something similar, you must use the latest version of Devise on github. Modify your Gemfile to say :

gem "devise", :git => "git://github.com/plataformatec/devise.git"

Avatar

I used this trick a lot since you first published it.

Every time I needed some smart behaviour with stuff stored into a session I moved it into the session object.
- storing only the user_id in the session
- let the session retrieve the user and cache it
- sign out the user if he hasn't browsed for a few hours.

I am wondering how would you approach making the session smarter that could play well with rails 3 and warden.

My first thought is to include a SessionSmart module into the session hash, the second one to post about it here.

Avatar

@Semyon thank you for putting this comment. There's no way I would have figured this out.

Avatar

how to dynamic update another selector when a selector changes by ajax on ruby?
because there are not load all select info when page loaded, should call server to get another select reload when a selector changes.
e.g I used country_select and state_select plugins. I don't know how to make state_select dynamic update after country_select changes.

Avatar

How to use partials in diferent folder to nested forms? Thank you for brillant cast!

Avatar

This one was a little difficult to follow because you never tell us about when something is magic. For instance when you use the factory to create a user name an password is it magic that it uses your username and password or did it ignore it and create them using the "foo" name? When you test for the name and pass you provided it passes so I THINK it's using magic but I'm not sure. Especially since you specifically use the user.password for the verification thing.

It would seem to make more sense (if it isn't using magic) to do a: f.username = user.username || "Foo". But again it might already be doing that for us.

So, in the future, PLEASE point out the magic bits so it makes more sense. I've noticed you breeze over them a lot in all of your casts. Hopefully you accept this as constructive criticism.

Anyway, thanks a ton for the 'casts. They have been a great help in learning ruby and rails.

Avatar

May I suggest using http://www.kryogenix.org/code/browser/sorttable/

100% javascript solution that runs in the browser without the need to access the server.

You can use helper methods to keep the views clean too.

The only inconvenient is when using pagination (which is not the case in this episode).

Avatar

I'm a bit stuck converting the AREL methods back to Rails 2.3.8. I get the named_scopes bit, but I'm struggling with the self.total_on(date) and orders_chart_series methods. Can someone help by showing how this example can be converted for use in a Rails 2.3.8 project?