Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > BOOK: Beginning Ruby on Rails
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Ruby on Rails
This is the forum to discuss the Wrox book Beginning Ruby on Rails by Steve Holzner Ph.D.; ISBN: 9780470069158
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Ruby on Rails section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 24th, 2007, 11:51 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5 p, 151"select3" application

Hello all,

I've been working on tying select controls to a model. I downloaded the code from the internet, but it doesn't work. I was trying to create the "select3" application. I finally got it working, but I want to understand why it is working. I have included all of the code necessary to make it work.

Here is the look_controller.rb file that did NOT work:

class LookController < ApplicationController
  def at
    @data_array = params[:cruncher]
    @cruncher = Cruncher.new(@data_array)

    @data = @cruncher.crunch
  end

  def input
  end
end

And here is the look_controller.rb file that I modified to work:



Here is my model cruncher.rb:

class Cruncher
  attr_accessor :crunch
  def initialize(data)
    @crunch = data
  end
end

Here is my controller look_controller.rb:

class LookController < ApplicationController
  def at
    @data_array = params[:cruncher]

    @cruncher = Cruncher.new(@data_array[:crunch])

    @data = @cruncher.crunch
  end

  def input
  end
end

The difference is that I added "@data_array[:crunch]" instead of just @data_array. I don't understand why this works, but it does. Can anyone explain it to me? Here is the rest of the code in case you want to refer to it or test it out:

Here is my input.rhtml file:

<html>
  <head>
    <title>Multiple select list</title>
  </head>

  <body>
    <h1>Multiple select list test</h1>
    <br />
    <br />
    Choose one or more ice cream flavors:
    <br />
    <br />
    <%= start_form_tag({:action => "at"}, {:method => "post"}) %>
      <br />
      <%= select("cruncher", "crunch", {'chocolate fudge' => 'chocolate fudge', 'vanilla bean' => 'vanilla bean', 'rocky road' => 'rocky road'}, {}, {:multiple => true, :size => 3}) %>
      <br />
      <br />
      <input type="submit" />
      <%= end_form_tag %>
    </body>
  </html>

And here is my at.rhtml file:

<html>
  <head>
    <title>Results</title>
  <body>
    <h1>Results</h1>
    <br />
    <br />
    Here are the results of your choices:
    <br />
    <br />
    You want to eat
    <% for data in @data %>
      <%= data %>
    <% end %>
    ice cream flavors.
    <br />
    </body>
  </html>


Thanks!
  #2 (permalink)  
Old July 27th, 2007, 05:20 PM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I understand it correctly, @data_array is a hash here. But as long as you do not assume it a simple string in cruncher.rb, the action "at" should still print something. The only thing is it will concatenate the hash key with the value. for example, if you enter "steve" in the text field, it will display "crunchsteve" (crunch is a key of the hash).



  #3 (permalink)  
Old August 19th, 2007, 08:45 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great! That answered my question and now I know why it works. Thanks!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 Page 151 angelojr BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 2 June 12th, 2007 08:15 AM
Chapter 5 page 151 aztek BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 4 July 27th, 2006 06:38 PM
Mistake in code on page 151 MythicalMe BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 May 22nd, 2006 01:11 PM
Beg. JSP 2.0 Chapter 16 - Struts web application EdgarRaul JSP Basics 2 August 13th, 2004 01:26 AM



All times are GMT -4. The time now is 12:03 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.