# Passing a hash to a view

**URL:** <https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696>\
**Category:** Rails\
**Created:** [August 22, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696 "2010-08-22T16:34:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 22, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/1 "2010-08-22T16:34:25Z")

</div>

Hello everyone,  
New to programming and hoping someone can help with this. I’m trying to  
format a hash in a model so I can access the individual values in a view  
as follows. (I pulled most of the code out but this is the basic  
idea)…

def set\_format\_for\_presentation # This method is in the model.  
answer\_list = [  
[self.incorrect\_ans\_1, self.incorrect\_anno\_1,  
self.incorrect\_ans\_pict\_1],  
[self.incorrect\_ans\_2, self.incorrect\_anno\_2,  
self.incorrect\_ans\_pict\_2]  
.shuffle

```
   # Create hash of virtual attributes.
   formatted = {
                 :anno_1 => answer_list[0][1],
                 :anno_2 => answer_list[1][1],
                 :anno_pict_1 => answer_list[0][2],
                 :anno_pict_2 => answer_list[1][2]}

```

end

So how do I pass the hash “formatted” to either a controller or access  
the values (:anno\_1 ect…) in the view?

Any help pointing me in the right direction would be greatly  
appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 22, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/2 "2010-08-22T17:49:10Z")

</div>

Hi Dave,

On Sun, Aug 22, 2010 at 9:34 AM, Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello everyone,  
> New to programming

Welcome and congratulations on your choice of language / framework !

> ```
> [self.incorrect_ans_2, self.incorrect_anno_2,
> 
> ```
> 
> end
> 
> So how do I pass the hash “formatted” to either a controller or access  
> the values (:anno\_1 ect…) in the view?

Ruby methods return the value of the last statement they execute. For  
idiomatic ‘correctness’ you should probably either remove the  
‘formatted =’ assignment of your hash or insert ‘formatted’ as the  
last statement in the method.

You’ve defined set\_format\_for\_presentation as an instance method so  
the assumption is that in your controller you have an instance  
variable of this class. To get the hash into an instance variable for  
use in your view…

in your controller:

@instance\_variable\_to\_use\_in\_view =  
@original\_instance\_variable.set\_format\_for\_presentation

and in your view

\<%= @instance\_variable\_to\_use\_in\_view[:anno\_1] %\>  
\<%= @instance\_variable\_to\_use\_in\_view[:anno\_2] %\>

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 24, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/3 "2010-08-24T18:30:51Z")

</div>

Thanks Bill,

I am really enjoying Ruby and Rails.

This was a HUGE help. Would have replied sooner but kept getting  
stopped by a spam filter I guess.

I appreciate your assistance

Thanks!

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 24, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/4 "2010-08-24T19:02:43Z")

</div>

On Sun, Aug 22, 2010 at 7:34 AM, Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> New to programming and hoping someone can help with this. I’m trying to  
> format a hash in a model so I can access the individual values in a view

> def set\_format\_for\_presentation # This method is in the model.  
> answer\_list = [  
> [self.incorrect\_ans\_1, self.incorrect\_anno\_1,  
> self.incorrect\_ans\_pict\_1],  
> [self.incorrect\_ans\_2, self.incorrect\_anno\_2,  
> self.incorrect\_ans\_pict\_2]  
> .shuffle

While I don’t know your whole use case, I’d suggest that this isn’t very  
Rubyish/OO.

Your “answer\_list” implies there’s such a thing as an “answer” – so  
why not make an Answer model with those attributes? Then you can  
have a collection of answers as easily as

@answers = Answer.all # or some subset as desired…

And `self.incorrect_ans_pict_1`, `self.incorrect_ans_pict_2`, etc. is  
just not a scalable pattern.

## FWIW!

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 25, 2010, 3:06am UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/5 "2010-08-25T03:06:32Z")

</div>

Thanks Hassan,

I’m new to programming so that seems daunting. I assume I would have to  
split my database into a “questions” and related “answers” table  
(currently questions and answers are attributes of “Question” in the  
same model. Then create an “answers” model, and then display the data  
from the two models in one view.  
Is this the correct design process?

Thanks, Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 25, 2010, 4:27am UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/6 "2010-08-25T04:27:14Z")

</div>

Dave C. wrote:

> Thanks Hassan,
> 
> I’m new to programming so that seems daunting.

What seems daunting? Since you didn’t quote the text you were replying  
to, there’s no way to know.

Anyway, bad design will be more daunting. Programming is no place for  
Ñ€ussyfooting around – that’s why you have automated tests and version  
control (you _do_, right?). Go slowly, test and commit all the time,  
and be bold!

> I assume I would have to  
> split my database into a “questions” and related “answers” table  
> (currently questions and answers are attributes of “Question” in the  
> same model. Then create an “answers” model, and then display the data  
> from the two models in one view.  
> Is this the correct design process?

Most likely. I highly recommend reading about DB normalization  
(Wikipedia is a good place to start).

> Thanks, Dave

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 25, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/passing-a-hash-to-a-view/193696/7 "2010-08-25T18:29:35Z")

</div>

On Tue, Aug 24, 2010 at 6:06 PM, Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

(sorry for the delay in responding, driving from Chicago to San Jose)

> I assume I would have to  
> split my database into a “questions” and related “answers” table  
> (currently questions and answers are attributes of “Question” in the  
> same model. Then create an “answers” model, and then display the data  
> from the two models in one view.

Not necessarily. There was no indication of the name of the model in  
your first post, but an “answer” and answer-related items could  
certainly  
be attributes of a question. However, if one Question can have more than  
one Answer (as answer\_list implies), then I think it’s a separate model.

Regardless, anytime you see something like your example

[self.incorrect\_ans\_1, self.incorrect\_anno\_1,  
self.incorrect\_ans\_pict\_1],  
[self.incorrect\_ans\_2, self.incorrect\_anno\_2,  
self.incorrect\_ans\_pict\_2]

with hardwired numbers, it’s probable there’s a better way to do it 🙂

It’d probably be helpful to read a bit on OO theory, as well as looking  
at Rails examples and tutorials.

## HTH, and good luck!

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan
