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!
|