Learning Rails show

Learning Rails

Summary: Want to learn how to build web sites with Ruby on Rails? Learning Rails starts from the beginning and teaches all the core concepts.

Join Now to Subscribe to this Podcast
  • Visit Website
  • RSS
  • Artist: LearningRails.com, Michael Slater and Chris Haupt
  • Copyright: Copyright 2007 Collective Knowledge Works, Inc.

Podcasts:

 8: Setting Up Your Development Environment | File Type: audio/mpeg | Duration: 00::00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails). To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Note: we created this series in 2008, and the Ruby on Rails world, along with the software that supports it (such as various gems) has evolved quite a bit since then. Check the comments for suggestions on how to overcome various incompatibilities. We hope to have an updated version of the series by the end of 2010. Detailed Ruby on Rails Development Environment Installation Instructions Mac OS X 10.5 Leopard Mac OS X 10.4 Tiger Windows Vista Windows XP

 7: Testing Rails Code | File Type: audio/mpeg | Duration: 00:20:00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails). To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Code Examples Here’s an example of using asserts. Suppose you have a test that searches for a non-existent podcast episode; you’d want to assert that the result was nil. The test case code would read: def test_podcast_does_not_exist podcast = Podcast.find_by_title('Intro') assert_nil podcast end Another example checking to see if the test database contains three podcasts loaded from fixtures might read: def test_three_podcasts_exist assert_equal, Podcast.find(:all), 3 end Here’s an example of a fixture, illustrating how fixtures can use model associations. An entry in the podcast.yml file might read: episodeone: name: Introductory Podcast listeners: anne, bob, charlie and entries in the listeners.yml file would read: anne: name: Anne podcast: episodeone bob: name: Bob podcast: episodeone charlie: name: Charlie podcast: episodeone Links to More Resources See the following sections of the site for links to resources on testing: Testing in general Test-driven development Unit testing Functional testing Integration testing Test fixtures Testing Toolsl Behavior-Driven Development

 6: Tools for Rails Developers | File Type: audio/mpeg | Duration: 00:30:00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails). To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Lesson Notes For links to more information about the various tools mentioned in this lesson, refer to following parts of the BuildingWebApps site: IDEs Editors Gems Automation Plugins Source Code (Version) Control Deployment Documentation Logging Debugging Testing

 5: Rails Form Processing | File Type: audio/mpeg | Duration: 00:18:00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails).   To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Lesson Notes CRUD is the acronym for the four main things you do with database records: Create Read Update Delete These notes illustrate the code described in the podcast. See the transcript for more details. For more details about forms processing with Rails, see the forms page in our topics section. You’ll find some great screencasts listed there that will go into much more depth than this podcast. For a database of podcast episodes, with a title and a description field for each, the basic form code looks like this: <% form_for @podcast do |form| %> <%= form.text_field :title %> <%= form.text_area :description %> <% end %> The minimal code for the controller’s create action is: Podcast.create params[:podcast] The create method takes the parameters from the params[:podcast] hash, uses them to initialize a new object, and then saves that object to the database. This line to the podcast model ensures that podcasts can’t be saved with a blank title: validates_presence_of :title In the controller, the create action tests the value returned from the Podcast.create call, and if it is not false, it knows the save succeeded. If it is false, then the form is rendered again. if Podcast.create params[:podcast] redirect_to (wherever you want to go after a successful save) else render :action => :new end To display the error messages, the form view just includes a call to the error_messages_for helper: error_messages_for :podcast To make this an Ajax form, we can simply change the form_for helper to remote_form_for: <% remote_form_for @podcast do |form| %> The rest of the form remains the same. The controller and views must be modified to return just an HTML or JavaScript snipped, rather than an entire page, after a create or update action.

 4: Rails Models | File Type: audio/mpeg | Duration: 00::00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails).   To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Resources for Further Study Any Ruby on Rails book will explain how migrations, models, and associations work. The classic Agile Web Development with Rails does a good job explaining them. Note that Rails 2.0 provides a slightly simpler syntax for migrations, which we’ve used in our examples, so in most books you’ll see the Rails 1.2 style. The Rails wiki has an explanation of migrations, but as of this writing it has not been updated for Rails 2.0. The API document provides a less readable but more complete and up-to-date description. There’s a hand-on tutorial using NetBeans 6.0 that shows how to create migrations and models using the NetBeans IDE. Code Examples The user requirements for the example we discuss in this podcast are: A visitor to the site sees a list of podcasts with their title, description, and a link to an MP3 file If there is an associated show note for that podcast, a link appears that goes to a page displaying it. Each podcast is tagged with one or more topic categories which the visitor can see next to the podcast’s other information Here’s a schematic drawing of the required objects: The migration that creates the podcast database table reads something like this: create_table "podcasts" do |t| t.string "title" t.string "description" t.string "filename" end The create_table line begins a code block that defines the table. This line provides the shortcut name t, which is referenced in the lines that define the table. To create a new podcast object, you use a very simple Podcast class and simply type: episode = Podcast.new And now you have an instance of the Podcast class, which you’ve named episode. Now, to set its attributes, you can simply write: episode.title = "Learning Ruby on Rails" episode.filename = "learningrails-1.mp3" episode.description = "This episode covers..." When this code is executed, you’ve created the episode object and filled it with data. Now it takes one more line of code, episode.save, to write the data to the database. To find an episode by name, you can simply write: episode = Podcast.find_by_title('Learning Ruby on Rails') and you’ll get back a podcast object, called episode, with all the information about that podcast. What about the show notes? We need another migration to define that table, the core of which is simply: t.text "body" t.integer "podcast_id" The body field is where we store the text of the show notes. (The column type of “text” instead of “string” tells the database to allow a potentially large amount of text.) The podcast_id field is how Rails knows what podcast a particular show_notes object is associated with. To set up the association, you add one line of code to each of the associated models’ classes. The podcast class gets a line that reads: has_one :show_notes and the show_notes model gets a line that reads: belongs_to :podcast With these two simple additions, you can now write episode.show_notes to access the show notes for a particular episode. To associate podcasts with categories, we need a join table, whose field definitions are: t.integer podcast_id t.integer category_id With the join table created and the HABTM declarations in the two model files, you can simply write episode.categories and you’ll get back an array of category objects, one for each category that is associated with that particular episode.

 3: Rails Views -- How Rails Renders Pages | File Type: audio/mpeg | Duration: 00:22:00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails). To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Code Examples In this podcast, we mention several bits of code that are hard to visualize in audio, so we’ve included them here. Refer to the transcript to see these examples in context. For links to additional resources, scroll down past the code examples. Embedded Ruby Ruby code in a view template is preceded by <% and followed by %>. The enclosed text is what is called “embedded Ruby,” or ERb. If the Ruby code is preceded by just <%, then the code is executed, but its output is not inserted into the HTML stream. For example, you might have Ruby code that says something like “if podcast title is not blank”. In real code, this would be: <% if !podcast.title.blank? %> For example, if we had a variable named title and we wanted its value to be inserted into the HTML, we’d write <%= title %> Last episode, we gave as an example a page listing all the episodes of this podcast. In this example, the variable that would be passed to the view would be an array of podcast objects. The code to do so looks like this: <% for podcast in podcasts %> <h2><%= podcast.title %></h2> <p><%= podcast.description %></p> <% end %> Partials A partial is just a small view template that is meant to be included in another template, or in a layout. The statement that goes in the controller is: render :partial => 'partial_name' You can also use a partial when you want to repeatedly insert the same bit of markup, but using data from a series of objects. For example, consider our listing of podcast episodes. We can use a partial that displays the information for a single podcast. You could simply put that partial in a loop that executes it once for each podcast object, like this: <% for podcast in podcasts %> <%= render :partial => 'podcast' %> <% end %> (For simplicity, we’re ignoring here some details about how the podcast variable is passed to the partial.) But Rails gives us a shortcut that makes this even simpler; in the “render partial” statement, we can refer to the array of podcast objects (which the controller provided), and the partial will automatically be repeated for each podcast in the array. Here’s the code: <%= render :partial => 'podcast', :collection => podcasts %> Rails Helpers Instead of writing: <a href = 'some_URL'>Text to be linked</a> you can use the Rails link_to helper, like this: <%= link_to 'Text to be linked', 'some_URL' %> The link_to helper is more interesting when you use it to help you generate the URL, instead of specifying it explicitly. For example, you can specify the controller and action you want the link to invoke, and any parameters that you want to pass to it, and let link_to generate the URL. Dynamic generation of links is really preferable when it comes to flexibility and maintenance of your code. For example, you could write: <%= link_to 'View Transcript', :controller => 'podcast', :action => 'view_transcript', :id => podcast %> Another common helper is image_tag. Instead of the usual <img src = '/images/filename'> you can write Ruby code that reads <%= image_tag 'filename' %> Rails assumes, by default, that all images are in a directory called images, so you don’t need to include the directory as part of the filename. You can also add additional parameters, such as alt text and image dimensions, using Ruby instead of HTML code, such as: <%= image_tag 'filename', :alt => 'Alt text goes here', :height => 50, :width => 125 %> DHTML with Rails Here’s a simplified version of the code to observe the shipping adddress checkbox and call as JavaScript function when it is checked: <%= observe_field 'shipping_address', :function => 'show_shipping_address();' %> (We haven’t shown here the code that defines the JavaScript function show_shipping_address(), which you could write in conventional JavaScript.) With RJS (Ruby JavaScript), you can write something like: <%= observe_field 'shipping_address', :function => update_page {|page| page[:shipping_address].show} %> There’s a bit of Ruby syntax there that we haven’t explained, but hopefully you get the idea. We’ve also ignored, for simplicity, the need to hide the shipping address if the box is unchecked. Ajax with Rails Here’s the observe_field statement that makes an Ajax call to the server to get the address: <%= observe_field 'address_chooser', :update => 'shipping_address', :url => {:controller => 'user', :action => 'get_address'}, :with => 'address_chooser' %> Resources for Further Learning See the Lesson Page for lesson 1 for links to Ruby and Rails books and other resources. If you want to get deep into the Ajax aspects of Rails, there’s one book devoted to that subject: Ajax on Rails To learn more about the JavaScript frameworks included in Rails, visit their sites: Prototype Scriptaculous

 2: Anatomy of a Web Application | File Type: audio/mpeg | Duration: 00:17:00

To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails).   To read a transcript of the lesson, click the Transcript link on the left. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. Examples used in the audio program Here’s some of the examples we use in the show, which may be easier to grasp in printed form: If the user types www.BuildingWebApps.com/podcast.html into their browser, they’re asking the server at BuildingWebApps.com to find a file called podcast.html and send the contents of that file to the browser. In a Rails application, the request would be written as www.BuildingWebApps.com/podcast, in which podcast is the name of the controller to be invoked A Rails URL such as www.BuildingWebApps.com/podcast/show/17 invokes the show action in the podcast controller and passes it the parameter of 17 (presumably the ID for a podcast episode). To request all the podcast objects, the controller would execute a statement such as podcasts = Podcast.find(:all). (Don’t worry about the odd punctuation for now. To find a particular podcast episode according to its episode number, the show action might execute a statement such as podcast = Podcast.find_by_episode(2) For More Information See the notes for Lesson 1 for pointers to online resources and books about Ruby and Ruby on Rails.

 1: Why You Should Learn Ruby on Rails | File Type: audio/mpeg | Duration: 00:22:00

Welcome to the Learning Rails Online Course If you’ve read this far, you probably already have an interest in building web applications with Ruby on Rails. But just in case you’re unsure if Rails is worth learning, in this lesson we explore the reasons for the success of Ruby on Rails and the benefits that can result from using it. The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes. To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails). To read a transcript of the lesson, click the Transcript link on the left. Web resources Keep in mind the distinction between Ruby and Ruby on Rails: Ruby is a programming language Ruby on Rails (often called simply “Rails”) is a framework for building web applications with Ruby The central web resources for Ruby and Rails, respectively, are: Ruby Language Ruby on Rails If you want to do some more preparation for your Ruby on Rails education, we highly recommend that you begin learning Ruby, the programming language, without regard to Rails (at first). You don’t need an extensive knowledge of Ruby, but you need to learn the basics. We’ll talk about Ruby a little more in later lessons, but you can get started now learning it on your own. If you have experience with another programming language, much of Ruby will seem very natural. If you don’t have any experience with object-oriented programming, you may want to read up on Ruby objects — in Ruby, everything is an object, and you’ll work with them constantly. Ruby uses one unusual construct, the code block, which is unfamiliar to most programmers and merits some study. Code blocks are used extensively in Rails. Ruby-lang.org has a good introductory tutorial and a longer introduction to Ruby. Ruby Essentials is a short online ebook on ruby. And for those of you who are more graphically inclined, check out Why’s Poignant Guide to Ruby, it even has foxes! You can begin experimenting with Ruby right in your browser, without having to install anything. Just browse to tryruby.hobix.com. Introductory books You need to learn about both Ruby and Rails, and you’ll want separate books for each of them. For beginning Ruby, these are both good introductory books: Learning Ruby Beginning Ruby There are several other valuable references and advanced guides for Ruby, which you’ll want to pick up if you get serious about your Ruby programming; see our Ruby bookstore for an extensive list. For learning Ruby on Rails, the current selection of books is a little problematic. Two years ago, there were only a few books; now there are dozens, which makes it more confusing to pick one. And alas, most of them are written for Rails 1.2.6, and the ones that are the most up-to-date are not well suited for beginners. Although it is not the most accessible book for a novice programmer, and is written for Rails 1.2.x, Agile Web Development with Rails remains the classic Rails book. But if you’re using Rails 2, which we recommend you do, parts of this book will be confusing due to changes in the framework. The most up-to-date and in-depth book on Ruby on Rails is The Rails Way. It isn’t written as an introductory book, so while we highly recommend it as a reference, you’ll probably want to start with another book to get your bearings first. The Agile book mentioned above is a good place to start, especially if you have a programming background. If you’re looking for a gentler introduction, some of the available books include: Rails Solutions: Ruby on Rails Made Easy Build Your Own Ruby on Rails Web Applications Beginning Rails Other books provide a lot of example code that provide useful guides for how to implement various features. Books of this type include: Practical Rails Social Networking Sites RailsSpace: Building a Social Networking Website with Ruby on Rails Beginning Ruby on Rails E-Commerce Rails Cookbook Rails Recipes See our complete list of Ruby and Ruby on Rails Books for more.

Comments

Login or signup comment.