RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Hey Ryan, love the screencasts. Quick question... I noticed in this one that as you typed keyboard shortcuts they were displayed in the upper right corner of the screen. Just wondering how you did that. Is this a feature of Textmate?

Avatar

lovely technique. can't believe how simple it is when you know the trick! replaced a horrible, horrible hack job i'd written in my ignorance.

so .. any hints on how you'd paginate that page you generate?

thanks for the wonderful videos, they're really great.

Avatar

Thanks for donating Rafael! It really helps. I enjoy making the screencasts as well.

Avatar

Hi
thanks a lot for your work. Thats really what I was looking for, made a !little! donation today, hope that I can continue to donate :-D

Hopefully you have a lot of fun by doing that and you continue making such great "snippets".

greetinx

Avatar

@Pimpmaster, really? I've used non double quoted strings before. What kind of variable is @article.title? If it's not a string, that may be why. You may want to do the "to_s" conversion inside the title helper method.

Avatar

Woops! Ignore those = on the ERB tags.

Avatar

I just realized one caveat here. This particulat helper only takes a double-quoted string, so something like:

<%= title @article.title %>

Does not work because you will explicitly have to set it to a string

<%= title @article.title.to_s %>

This is not the case if you use the classic instance variable solution.

Avatar

another great screencast! After every casts I have to go through all my code and change/implement what I've just learned ;-)

I'd like to see more casts where you use helper methods, cause I think they are useful but I don't know when...

cheers

Avatar

This is one of my favorite screencasts. I just redid all my views and they sure look purty!

Cheers!

Avatar

Yep, Zach's correct. Ruby gets confused when you don't wrap the :title parameter in parentheses so it spits out a syntax error. I'm guessing it's because it thinks the {} brackets are trying to represent a hash not a block.

Same goes for later on in the video where I use parenthesis when calling yield(:title) while adding the || symbol. This is because Ruby doesn't know where to put the parenthesis, it could be here:

yield(:title || "foo")

or here:

yield(:title) || "foo"

It defaults to the first one (IIRC) so we need to clarify.

Whenever there's any doubt, wrap the parameters in parenthesis.

Avatar

Rob,

He says so during the screencast. It's due to the syntax reading past :title, I believe.

So use it with brackets for now.

Avatar

Another great tutorial Ryan!

Avatar

I was wondering why..

content_for(:title) { page_title }

works fine but

content_for :title { page_title }

doesn't - it gives a 500 error. Sorry.. I'm new to Rails/Ruby.

Avatar

Hi! Could you make a cast for i18n and l10n? would be nice!

thanks! congratulations!

Avatar

even better

<%= options_from_collection_for_select((Date.const_get :DAYS).sort_by(&:first),'last','first',@preference.autosendday) %>

Avatar

Hi I watch this railscast religiously. It has taught me a lot. I really appreciate the excellent explanations for why you are doing something not only in rails but with ruby in general. I just spent about three hours figuring this little thing out so I figured i'd post it.

My application calls for a day of the week select box. there is no real helper for this. Here is the code I use..

<select>
<% dates = Date.const_get :DAYS%>
<%= options_from_collection_for_select(dates.sort{|x,y| x.last<=>y.last},'last','first',@preference.autosendday) %>
</select>

hope this helps someone save a minute of there programming life

Avatar

Great idea Jake. I normally don't like giving methods a dual purpose, but in this case I think it works very well.

Avatar

Great screencast! I took your idea one step further and had my title helper return either the set title or a default if one isn't already set when called with no arguments. That way I can call the helper to set the title, or call it w/ no arguments when I want to use the title.

Avatar

This screencast is useful for me. Can you create screencast about using HTML Tidy in Ruby on Rails? Thank you!

Avatar

Used @page_title all the time :)

Avatar

I really love your blog. Keep your good job job! Congratulations!

Avatar

@Clayton, hmm, not sure why it's fuzzy. It sounds okay here and I didn't do anything different. Oh well, hope it's not a regular occurrence.

@jjk2, I'm using the Animation compression setting in QuickTime. The secret is to set the keyframe rate way up (about 1500) and it should make decent size files.

Avatar

ryan, how r u able to make ur screencasts so high quality, yet file size so small? can this be achieved with formats other than .mov ? what program do you use ?

Avatar

The sound seemed a little "fuzzy" on this one... other than that I've learned ANOTHER new something about rails. Thanks!

Avatar

@Steven, the reason I chose beginning_of_month is because it allows you to easily format the month however you want in the view. You can use the short month name, full month name, include the year, etc. The "month" method just returns the month as an integer so you lose the year information.

Avatar

Instead of grouping by date.beginning_of_month, you could just group by date.month :)

Avatar

Ah, much better. We should pair program more often.

Avatar

Just found this which I think is the best solution:

@task_months.sort_by(&:first).each do |month, tasks|

Edit: Actually, it looks like "sort" by itself automatically sorts by key:

@task_months.sort.each do |month, tasks|

Avatar

I like that idea, perhaps sort_by_keys would be appropriate.

Avatar

Hi Carl, I knew there must be another way to do that. Thanks for the tip!

I'm not sure if it's any cleaner than the other hack, but I can imagine extracting this into a custom Hash method to keep the view nice and clean.

Avatar

I don't think you need that hack at the end, you should be able to do:

<% @task_months.sort { |a,b| a[0] <=> b[0] }.each do |month, tasks|

When you call sort on a hash it will just convert it to an array and do what you originally wanted.

Avatar

Wooow, I have no words !!!

Avatar

Great stuff. Gonna have to watch this one a few times :-)

As always, awesome job!

Avatar

In the last example where you use in_groups_of(4, false) does that yield valid X/HTML? ie. the last table row will not have as many TD elements as the previous rows. Don't know if that's allowed or not (it may be).

Avatar

Great episode as always

Avatar

Really a good tip!

I love your blog. Congratulations.

Avatar

I find the ipod versions hard to read because they're too blurry (the normal versions are fantastic, incredibly clear!).
I suppose that's because the resolution has been reduced, but is it also because of higher compression? h264 should be able to do clear video, is it possible to turn up the bitrate?

Anyway, these railscasts are very useful, keep up the great work!

Avatar

perfect!
完美!

Avatar

@Sam, the difference is subtle but very important. I'll try to dedicate a screencast to it in the future.

In short, normally you will have a more robust interface for a has_many :through association because the join model may likely have more fields than the two foreign keys. So you may need a checkbox and a text field for example. Usually this problem is solved with AJAX so it's not just simple checkboxes. I'll try to explain more in a future episode. Thanks for the suggestion

Avatar

@Enrique, good question. Thankfully Rails automatically quotes and escapes the input variable for the dynamic find by methods, so it is just as secure as using the full conditional statement with the question mark.

Avatar

Thanks for your good work! Your screencasts are a real time saver.

Avatar

Hi, I saw this railscast a while back and I've been meaning to ask something.

I like the dynamic find_by methods, but don't you lose the protection you get by the use of the question mark followed by variables?

Is "Task.find_all_complete(false)" less secure than "Task.find(:all, :conditions => ['complete = ?', false])" ? For example, if we were to plug a user-supplied value as the argument tot he dynamic find_by methods?

Avatar

Hey Ryan, these screencasts are really nice, you're doing some really good work :-)

It seems that there's a bit of confusion about the differences between HABTM and has_many :through, particularly with these checkboxes. Maybe you might spend a bit of time explaining the differences, and/or running through the process of implementing these category checkboxes with a has_many :through relationship.

Thanks for the effort you're putting into this :-)

Avatar

your screencasts are short, extremely useful, understandable and kinda cool even.

Avatar

I enjoy watching your shows. Thanks from Germany!

Avatar

@InMan, thanks. I'll fix that.

@eTrueke & Rebort, if CSS will work for you, then that is definitely preferred. But sometimes you need the exact behavior only a table can provide, where you need everything to line up and you don't want the columns to be fixed width. I'm no CSS guru so I may just be missing something.

@Slaptijack, right, this was more of a way to show off in_groups_of then to push tables. You might, for example, need to use it while generating a PDF document where the rows and columns need to be strictly defined.

Avatar

Although it might be easier to accomplish this particular task via CSS, I think it's also a simple way to show the functionality of the in_groups_of() method.

Avatar

I am addicted to your site. Great tips once again!

I remember creating this sort of fucntionality in PHP about eight years ago -- pre CSS days and all -- and it was a bit of a nightmare.

What's the advantage, though, of using Rails for this instead of formatting with CSS?

Avatar

Hey Ryan.
Just for info: Your add comment labels have mistype.
<label for"comment_name" class="required">Name:</label>
Should be for="comment_name"

Thanks for railscast :).