passing a hash to a view

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.

Hi Dave,

Hello everyone, New to programming

Welcome and congratulations on your choice of language / framework !

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?

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

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

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!

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

Dave Castellano 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,

(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 :slight_smile:

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!