{"id":26,"date":"2005-12-03T16:30:29","date_gmt":"2005-12-03T21:30:29","guid":{"rendered":"http:\/\/blog.elharo.com\/blog\/?p=26"},"modified":"2008-05-29T10:29:24","modified_gmt":"2008-05-29T15:29:24","slug":"birds-on-rails-part-3","status":"publish","type":"post","link":"https:\/\/www.elharo.com\/blog\/software-development\/web-development\/2005\/12\/03\/birds-on-rails-part-3\/","title":{"rendered":"Birds on Rails, Part 3"},"content":{"rendered":"<p>Now I want to create a new page that lists all the species in the birds table. That is, it looks like this:<\/p>\n<blockquote>\n<h1>Brooklyn Bird Report Master Bird List<\/h1>\n<ul>\n<li>Fulvous Whistling-Duck  <i>Dendrocygna bicolor<\/i><\/li>\n<li>Greater White-fronted Goose  <i>Anser albifrons<\/i><\/li>\n<li>Snow Goose  <i>Chen caerulescens<\/i><\/li>\n<li>Ross&#8217;s Goose  <i>Chen rossii<\/i><\/li>\n<li>Brant  <i>Branta bernicla<\/i><\/li>\n<p>&#8230;\n<\/ul>\n<\/blockquote>\n<p>This will live in the file \/masterbirdlist.rhtml<\/p>\n<p>How to do it? I begin by creating a regular HTML file named masterbirdlist.rhtml and putting it in the app\/views\/bbr directory, but that doesn&#8217;t work. It just tells me &#8220;Unknown action No action responded to masterbirdlist.rhtml.&#8221; OK. Looking back at the notes from Day 1, I see how to handle that. I add this method to the <code>bbr_controller.rb<\/code> file:<\/p>\n<pre>def masterbirdlist\r\n  render_text \"Hello\"\r\nend<\/pre>\n<p>Now I get &#8220;Hello&#8221; for the masterbirdlist. Of course that&#8217;s not what I want. I want to load the masterbirdlist.rhtml file. How do I do that? About this point I begin wondering what the manual says. Googling for it, it appears that there isn&#8217;t one. That compares very unfavorably with PHP and Java, both of which at least have documentation, not to mention reams of third party books available. (Later I found <a href=\"http:\/\/manuals.rubyonrails.com\/\">this site<\/a>, but it doesn&#8217;t appear to be a true comprehensive manual; just a random collection of HowTos.) However, there does seem to be some <a href=\"http:\/\/api.rubyonrails.com\/\">API documentation<\/a> which mentions <code>render_file()<\/code> methods. Maybe that&#8217;s what I need? No wait. I remember this now. I bet I have to generate a controller for the masterbirdlist:<\/p>\n<pre>~\/Web sites\/bbr$ script\/generate controller masterbirdlist\r\n      exists  app\/controllers\/\r\n      exists  app\/helpers\/\r\n      exists  app\/views\/masterbirdlist\r\n      exists  test\/functional\/\r\n   identical  app\/controllers\/masterbirdlist_controller.rb\r\n   identical  test\/functional\/masterbirdlist_controller_test.rb\r\n   identical  app\/helpers\/masterbirdlist_helper.rb\r\n<\/pre>\n<p>That seems to work. I thought this was just for mapping to database tables, but apparently it&#8217;s for all pages. This gives me the &#8220;Unknown action No action responded to index&#8221; at \/masterbirdlist. But I&#8217;ll create a view for this in app\/views\/masterbirdlist\/index.rhtml. Bingo! That works.<\/p>\n<p>Now let&#8217;s add the code to generate the list of species:<\/p>\n<pre>&lt;ul>\r\n\r\n &lt;% @birds.each do |bird| %>\r\n  &lt;li>&lt;%= bird.common_name %>&lt;\/li>\r\n &lt;% end %>\r\n&lt;\/ul><\/pre>\n<p>This produces <\/p>\n<blockquote>\n<h1>\n  NoMethodError in<br \/>\n  Masterbirdlist#index<br \/>\n<\/h1>\n<p>\n  Showing <i>app\/views\/masterbirdlist\/index.rhtml<\/i> where line <b>#14<\/b> raised:\n  <\/p>\n<pre><code>You have a nil object when you didn't expect it!\r\nYou might have expected an instance of Array.\r\nThe error occured while evaluating nil.each<\/code><\/pre>\n<\/blockquote>\n<p>So now I have to create the <code>birds<\/code> variable. Thus I add this code to the <code>masterbirdlist_controller.rb<\/code>:<\/p>\n<pre>def index\r\n  @birds = Bird.find_all\r\nend\r\n<\/pre>\n<p>Now I get this error:<\/p>\n<blockquote>\n<h1>\n  NameError in<br \/>\n  Masterbirdlist#index<br \/>\n<\/h1>\n<pre>uninitialized constant Bird<\/pre>\n<\/blockquote>\n<p>I suspect this is because I haven&#8217;t mapped that table in Rails yet. i.e. I haven&#8217;t generated a model for the birds. Let&#8217;s do that:<\/p>\n<pre>$ script\/generate model Bird\r\n      exists  app\/models\/\r\n      exists  test\/unit\/\r\n      exists  test\/fixtures\/\r\n      create  app\/models\/bird.rb\r\n      create  test\/unit\/bird_test.rb\r\n      create  test\/fixtures\/birds.yml<\/pre>\n<p>And now the page works. OK. I &#8216;m starting to get the hang of this. Adding the genus to the list is easy:<\/p>\n<pre>&lt;% @birds.each do |bird| %>\r\n  &lt;li>&lt;%= bird.common_name %>  &lt;i>&lt;%= bird.genus %>&lt;\/i>&lt;\/li>\r\n &lt;% end %><\/pre>\n<p>Now let&#8217;s add the species:<\/p>\n<pre> &lt;% @birds.each do |bird| %>\r\n  &lt;li>&lt;%= bird.common_name %>  &lt;i>&lt;%= bird.genus bird.species %>&lt;\/i>&lt;\/li>\r\n &lt;% end %><\/pre>\n<p>Oops. That didn&#8217;t work:<\/p>\n<blockquote>\n<h1>\n  ArgumentError in<br \/>\n  Masterbirdlist#index<br \/>\n<\/h1>\n<p>\n  Showing <i>app\/views\/masterbirdlist\/index.rhtml<\/i> where line <b>#15<\/b> raised:\n  <\/p>\n<pre><code>wrong number of arguments (1 for 0)<\/code><\/pre>\n<\/blockquote>\n<p>Hmm. Do I just need to put that in two separate tags or do I need to learn how to concatenate strings or terminate statements in Ruby? Concatenating seems to work; and it uses the plus sign, same as in Java:<\/p>\n<pre> &lt;% @birds.each do |bird| %>\r\n  &lt;li>&lt;%= bird.common_name %>  &lt;i>&lt;%= bird.genus + bird.species %>&lt;\/i>&lt;\/li>\r\n &lt;% end %><\/pre>\n<p>However, it turns out I forgot a space, so I might as well just go ahead and use two tags anyway:<\/p>\n<pre> &lt;% @birds.each do |bird| %>\r\n  &lt;li>&lt;%= bird.common_name %>  &lt;i>&lt;%= bird.genus %> &lt;= bird.species %>&lt;\/i>&lt;\/li>\r\n &lt;% end %><\/pre>\n<p>I think that&#8217;s as far as I&#8217;m going to go tonight, but just maybe I am speeding up a little past what I could do with PHP (though I still don&#8217;t fully grok how the site is getting organized, and the URLs mapped.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now I want to create a new page that lists all the species in the birds table. That is, it looks like this: Brooklyn Bird Report Master Bird List Fulvous Whistling-Duck Dendrocygna bicolor Greater White-fronted Goose Anser albifrons Snow Goose Chen caerulescens Ross&#8217;s Goose Chen rossii Brant Branta bernicla &#8230; This will live in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[410],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-web-development","tag-flash"],"_links":{"self":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":0,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}