#100
Apr 07, 2008

5 View Tips

Here's five quick tips to use in views. Be sure to catch the end of the episode where I reveal an exciting contest!
Tags: views
Download (25.9 MB, 9:25)
alternative download for iPod & Apple TV (14.3 MB, 9:25)

Tip #1: Whitespace in ERB Templates

Use a dash at the beginning and end of an ERB tag to remove the white space around it.

<div id="products">
  <%- for product in @products -%>
    <h3><%=h product.name %></h3>
  <%- end -%>
</div>

Tip #2: content_for :side

You can use the content_for method in your template to store up code to use later on in the layout.

<!-- index.html.erb -->
<% content_for :side do %>
  ...
<% end %>

<!-- application.html.erb -->
<div id="side">
  <%= yield(:side) || render(:partial => '...' %>
</div>

Tip #3: Debugging Variables in Views

Pass a variable to the debug method to get the full details.

<%= debug @products %>
<%= debug params %>
<%= debug request.env %>

Tip #4: The Different Form Helpers

Rails comes with a lot of different helper methods dealing with forms. Here’s a quick tip on deciding which ones to use. If the form is editing a model, use the helper methods which do not end in the word “tag”. If you aren’t editing a model (such as a search form) then do use helpers which end in tag.

<!-- index.html.erb -->
<% form_tag products_path, :method => :get do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

<!-- new.html.erb -->
<% form_for @product do |f| %>
  <p>
    <%= f.label :name %>
    <%= f.text_field :name %>
  </p>
  <p><%= f.submit "Create" %></p>
<% end %>

Tip #5: Optional Locals in Partials

If you want to make a :locals argument passed to a partial optional, you can move it into a helper method and give it a default value there.

<%= display_product @product, :show_price => true %>
# in helper
def display_product(product, locals = {})
  locals.reverse_merge! :show_price => false
  render :partial => product, :locals => locals
end

RSS Feed for Episode Comments 73 comments

1. amarrextro Apr 07, 2008 at 00:11

Congratulations!!!


2. Junhyun Apr 07, 2008 at 00:28

Another concise and fun episode !!! Looking forward to your next 100 episodes. Thank you from Korea.


3. DAddYE Apr 07, 2008 at 01:06

Congratulations!!!


4. jujudellago Apr 07, 2008 at 01:09

Happy 100th anniversary !!!
thank you so much for doing this :)

Cheers from Switzerland


5. Werner Apr 07, 2008 at 01:43

Thanks a lot. You helped me much to learn RoR and to be productive with it..
Greetings from Germany


6. Taylor Redden Apr 07, 2008 at 02:10

Congratulations Ryan! You really do some great work here. That contest is sick man, great move. I think its a great way to get people involved in the community. Keep em coming!


7. Roob Apr 07, 2008 at 02:38

5 tips that really help in daily work!


8. Thomas Apr 07, 2008 at 03:08

Hi Ryan,

What do you use to create your screencasts and especially what software displays the keyboard shortcuts? It doesn't look like mouseposé.


9. Morgan Roderick Apr 07, 2008 at 03:34

Congratulations!!

Great work Ryan!!!


10. Ricky Apr 07, 2008 at 04:02

Congratulations on 100 great tutorials. A really asset for the Rails community.


11. Peter Duijnstee Apr 07, 2008 at 05:15

100 episodes, time to break out the champagne! Congrats Ryan and thanks a ton for cleaning up my code ;)


12. Prateek Apr 07, 2008 at 05:24

I have been a fan of your screencasts for almost a year now ... congratulations on the 100th episode

Regards
Prateek Dayal


13. Martin Apr 07, 2008 at 05:46

You have a very nice collection of screencasts now, Ryan. I recommend them often, and I really like the short focused format.
Congratulations on episode 100!


14. mrb_bk Apr 07, 2008 at 06:20

Thanks a ton for all of your help and hard work in contributing to the Ruby and Rails communities. These screencasts are truly a blessing!


15. David Parker Apr 07, 2008 at 06:49

Great stuff Ryan. Congrats on the 100 episodes. Thanks again!


16. Sarah (aka: Squib) Apr 07, 2008 at 07:52

Great job bro and congratulations on your 100 episodes! I know you've worked really hard at it and it shows.

Your Proud Sister,
Sarah


17. nicolash Apr 07, 2008 at 08:20

Congrats...
and i like the new sound at the end of your intro... hope you didn't kill a mouse for this? ;-)


18. Polar Apr 07, 2008 at 08:35

Thank you for all the great tutorials. This is a fantastic site!


19. Dante Regis Apr 07, 2008 at 09:06

Hello, there Ryan!

I've been doing a GREAT work, and I'm pretty sure that almost every Rails developer out there has learned something from at least one of your videos.

They make big difference when facing common problems we all have during work, and releasing the 100th just proves that people like your tips and that they are important to many.

Keep up the good job, and most importantly THANK YOU!


20. Carl Apr 07, 2008 at 09:10

I have to second nicolash's comment. And here I was thinking you forgot our request. I just about burst out laughing when I heard the sound! Great contest BTW, I will have to see what I can come up with for an entry.


21. Zargony Apr 07, 2008 at 09:37

Congrats on your 100th episode. I've watched them since day one and learned a lot of stuff from it. These screencasts are definitely one of the best sources for any rails programmer.

Btw, ROFL about the intro sound. Will you keep it in future episodes? :-D


22. Gustavo Beathyate Apr 07, 2008 at 10:16

Congratulations man, great work.


23. Stephen Wooten Apr 07, 2008 at 10:21

You are one of those guys who makes the whole open source-ness of Ruby/Rails so successful as a community. Big thanks and keep it up!


24. Doug M Apr 07, 2008 at 10:36

"Congratulations! " * 100
"Thank You! " * 1.0e6


25. Aleksandr Apr 07, 2008 at 10:39

Thanks Ryan for your hard work! Will be happy to buy you beer if you're ever in Hamburg! :)


26. Pete Thompson Apr 07, 2008 at 10:49

Thanks for 100 great screencasts, Ryan!

Here's to another hundred! :)


27. Luigi Apr 07, 2008 at 13:20

Congratulation for your 100th screencast :)


28. sdr Apr 07, 2008 at 15:47

Congratulation!
Thanks again!


29. Nuwan Chaturanga Apr 07, 2008 at 19:54

Congratulations!!

You are a good presenter and an excellent teacher. I watched each of the episodes from day one and learnt a lot. Keep up the good work.

Thank you very much.


30. d Apr 08, 2008 at 01:05

Ryan, if you ever come to Sweden, don't even think about leaving before I get to thank you personally!


31. tiroxino Apr 08, 2008 at 03:55

The 100 most useful rails screencasts. Thank you!

Greetings from Spain.


32. Roy van der Meij Apr 08, 2008 at 04:04

Congrats!

You make me a better programmer, and you definitely make the monday morning a great morning.

Up to the next 100!


33. JayG Apr 08, 2008 at 04:07

Thanks and CONGRATULATIONS for your's 100 episodes. Keep the great work. You're really good on it and very easy to understand.


34. ilumine Apr 08, 2008 at 06:28

Ryan, seriously, you rock!
Concratulations for hitting the 100.
Keep on the good work, you helped me and others way more than any other rails resource (except the api of course ;) )
Greetings from Germany,

Pascal


35. Floyd Price Apr 08, 2008 at 09:01

Ryan, This really is a great achievment and an amazing contribution from you to this fantastic community, Thanks and Well Done!


36. Henrik N Apr 08, 2008 at 11:09

Didn't watch the screencast, only checked out the text examples, so you might have covered this:

Another way to deal with optional locals is to use <code>local_assigns[:the_local]</code> in the partial. Will be <code>nil</code> if not assigned.


37. Paul Goscicki Apr 08, 2008 at 12:06

I cannot get tip #1 to work. I still have those pesky newline characters. Is it available in Rails edge only?


38. Jared Dobson Apr 08, 2008 at 15:32

Amazing!!!!!! Thank you so much Ryan!!!!!!!! Amazing job!


39. Carl Apr 08, 2008 at 16:22

@Paul, did you add the minus sign on both tags? <%- ... -%> for example?


40. Paul Goscicki Apr 09, 2008 at 04:29

@Carl, sure:

&lt;%- 3.times do -%&gt;
&lt;p&gt;test&lt;/p&gt;
&lt;%- end -%&gt;

results in:

&lt;p&gt;test&lt;/p&gt;

&lt;p&gt;test&lt;/p&gt;

&lt;p&gt;test&lt;/p&gt;

I have no idea why...


41. Paul Goscicki Apr 09, 2008 at 04:29

Uhm, now not encoded:

<%- 3.times do -%>
<p>test</p>
<%- end -%>

results in:

<p>test</p>

<p>test</p>

<p>test</p>


42. Gavin Apr 09, 2008 at 05:48

Awesome work, Ryan! I'll be here waiting for every one of the next 100!!


43. DJ Apr 09, 2008 at 06:42

Thank you Ryan for your screencasts - I look forward to them each week. I have certainly learnt a lot from your tutorials.

I there any reason I can't get them onto my iPhone? I subscribed using iTunes, but I still can't get them onto the iPhone.


44. Wenderson Rodrigues Apr 10, 2008 at 04:38

Congratulations!!!
Great work!!!
Thank you from Brazil


45. Helmuth Apr 10, 2008 at 12:28

Thank you.
Keep on going!!
Greetings from Austria


46. Randy Connelie Apr 10, 2008 at 18:02

re: Optional Locals

I prefer to handle optional locals in the partial itself. I like how it keeps the relevant pieces together in one file.

Add code like this to the top of your partial:
<% if local_assigns[:local_value_name].nil? then local_value_name=default_value end %>


47. Andy Waite Apr 10, 2008 at 18:21

Thanks for all the great episodes.


48. Ben Karst Apr 10, 2008 at 19:14

Greetings and congrats from Portland!

I've been an employed rails developer for 6 months now and railscasts have been the most helpful and reliable resource I've come across.

Thanks Ryan!


49. Mikel Apr 10, 2008 at 21:09

Well done Ryan, fantastic job on your 100th Screen cast.

Here is my first tip :)

http://lindsaar.net/ruby-on-rails-tips/

Regards

Mikel


50. Fredrik Apr 11, 2008 at 01:03

Thanks for the great content on this website. I have learnt a lot from it!


51. Soleone Apr 12, 2008 at 01:11

Congratulations from Germany!

Love your podcast, they're very professional and also have a friendly touch. Keep up the good work, you sure are one of the Ruby Heroes! :)

See ya around, Dennis


52. Frederico de Souza Araujo Apr 12, 2008 at 11:42

Congratulations from Thailand!

Happy 100th podcast!!!

Thanks a lot for those Great Podcasts,

They are Truly AWESOME.


53. wibblecp Apr 13, 2008 at 12:57

congratulations from italy and thanks for your screencasts


54. Benjamin Apr 17, 2008 at 02:49

Hi,
Just a quick question, do we get a confirmation reply wen we submit our 5 tips to contest@railscasts.com? Just to make sure you really got the mail :) My mail sometimes get stuck in the spam folder :(

Thx


55. Ryan Bates Apr 18, 2008 at 20:16

Thank you everyone for your encouraging comments!

@Thomas, I'm using KeyCastr.
http://stephendeken.net/wiki/software/keycastr/

@DJ, there's two feeds, make sure you're using the iPod feed if you want it to play on the iPhone. :)

@Benjamin, great suggestion. I'll reply to each contest submission to confirm I received it.


56. jc Apr 20, 2008 at 08:23

Congrats on 100 Ryan! Thanks for so many fantastic videos.


57. Hamburg May 16, 2008 at 02:07

Congrats from me too. 100 screen casts and I watched almost every single one of them! Thanks a lot! Keep up the good work!


58. kino May 23, 2008 at 01:53

As is shown in the writings of Hume, philosophy, in respect of the intelligible character, abstracts from all content of knowledge.


59. Pablo Andres Jul 18, 2008 at 21:41

Hey!! Frederico de Souza I Pablo from Chile look for you and find you, I believe.
Someone know his your mail to contact it, he was a great friend.
my mail is: ramzeone@hotmail.com

THANKS!!


60. Friseur Aug 05, 2008 at 07:42

Nice tutorial. This has probably saved me a few hours of searching. Thanks!


61. Social Media Aug 16, 2008 at 05:32

Hot episodes!

Congratulations and thanks for all the great episodes guys

best regards


62. Bayerischer Wald Sep 14, 2008 at 02:24

thanks, really the easy way.
Greetings Bayerischer Wald


63. Ed Anderson Oct 11, 2008 at 09:26

Tip #5 (default value for locals)

An easier way is to do something like this in your partial:

show_price = false if !defined?(show_price)


66. black blowjob Dec 16, 2008 at 08:19

nice


66. Starcraft CD Keys Dec 17, 2008 at 17:22

thanks


67. Warcraft 3 CD Key Dec 17, 2008 at 17:51

ok


68. diablo 2 CD Key Dec 17, 2008 at 19:15

ok


68. IV Dec 22, 2008 at 01:35

Thanks for the great shows!


69. redding used autos Dec 26, 2008 at 07:12

I've watched them since day one and learned a lot of stuff from it. These screencasts are definitely one of the best sources for any rails programmer.
<a href="http://www.norcalcars.com/">redding used autos</a>


70. Weight Loss Goddess Dec 28, 2008 at 12:36

Great dude..
You are a good presenter and an excellent teacher. I watched each of the episodes from day one and learnt a lot. Keep up the good work.

Thank you very much.


71. Volkswagon Dec 28, 2008 at 20:48

Wow,Thats a great contribution from you.
Thank you.


72. top vampire books Dec 31, 2008 at 14:07

they're very professional and also have a friendly touch. Keep up the good work, you sure are one of the Ruby Heroes!
<a href="http://vampirelibrarian.com">top vampire books</a>


73. lowest prices Jan 03, 2009 at 23:18

Thanks a lot,Could you suggest me the best place where i can learn Ruby on Rails online?

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player