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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Dave, On Sun, Aug 22, 2010 at 9:34 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello everyone, > New to programmingWelcome 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Aug 22, 2010 at 7:34 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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] > .shuffleWhile 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, DaveBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Aug 24, 2010 at 6:06 PM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.