Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > BOOK: Beginning Ruby on Rails
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2007, 11:51 PM
Registered User
 
Join Date: Jan 2007
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!
 
Old July 27th, 2007, 05:20 PM
Registered User
 
Join Date: Jul 2007
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).



 
Old August 19th, 2007, 08:45 PM
Registered User
 
Join Date: Jan 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.