Leonel *-* wrote:
> I'm trying to display a drop-down menu by using an instance variable
> from a model.
[...]
> Now, I want to populate the values from the model like this
> @durations = {"30 minutes" => "30", "1 hour" => "60", "1 hour 30
> minutes" => "90", "2 hours" => "120"}
> 
> Then in the form partial I have this
> <div class="field">
>   <%= f.label :duration %><br />
>   <%= f.select ("duration", @durations.map {|d| [d.id, d.name]}, :prompt
> => "Select") %>

Why not just use collection_select?

And why not use Haml?

> </div>
> 
> I get this error:
> You have a nil object when you didn't expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.map

Of course you did!  The view has no access to model instance variables. 
If you want to use @durations in the view, you'll need to set it in the 
*controller*, not the model.  Rails copies controller instance variables 
to the view.

> 
> How can I initialize an instance variable or instance hash

No such thing as an "instance hash".  Did you mean an instance variable 
that happens to contain a Hash?

> and then use
> it on the form for the drop-down menu?

By setting it in the controller.  If this is at all confusing, you may 
want to review the Rails Guides or other basic reference.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to