I have a Survey-Response application, where surveys can be created with an arbitrary number of questions, and users can respond to an arbitrary number of surveys. A Survey has_many Questions and has_many Responses. Both a Question and a Response has_many Answers. That way, everybody has a relationship to everyone else. In the Response#new for each Response.survey.questions I do a @response.answers.build and set the questions_id attribute. That looks like this: 1: survey = Survey.find(params[:survey_id]) 2: @response = survey.responses.build 3: @response.survey.questions.each do |question| 4: @response.answers.build(:question_id => question.id) 5: end All I want to do in the form template is get the Answer''s Question''s text to display as a label. That looks like this: 6: <% f.fields_for :answers do |fields| %> 7: <p>Q: <%= Question.find(fields[:question_id]).ask %></p> 8: <p>A: <%= fields.text_field :content %><br /></p> 9: <% end %> Obviously Line 7 is junk, but that''s basically what I want to do. How do I get those prepopulated attribute values? I haven''t been able to find anything on this so it''s either obviously staring me in the face or an odd question. I found this similar question, but it hasn''t received a reply. http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/115dbb1ba9215b0f/33779ed24e48c26c?lnk=gst&q=attributes+form#33779ed24e48c26c Thanks in advance. Dee -- 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.
I haven''t had any responses in the past few days, so the problem must be unsolvable in Rails. ;) LOL But let me rephrase the question more succinctly. Assume, Shelf has a description and a set of items. Item has a description, sku and color. Given, I use build in my controller''s new method to create a set of models like this: @shelf = Shelf.new 3.times { @shelf.items.build(:sku => GenerateNewSku, :color => ''blue'') } Explain how to display the sku and color in the form for each item? <% form_for @shelf do |f| %> <%= f.label :description %> <%= f.text_field :description %> <% f.fields_for @shelf.items do |item_fields| %> <%# ****Want :sku here**** #%> <%# ****Want :color here**** #%> <%= item_fields.text_field :description %> <% end %> <%= f.submit %></p> <% end %> Why I want this: My app has Surveys, and each has many Questions. Each Question has many Answers (one per user). The Answers also belong to a Response, which belong to a User. Response can be thought of as the user''s answers to a Survey. If that''s not clear, imagine a data diagram in the shape of a diamond, with Survey at the top branching down into Questions and Responses at the next level, and Answers at the bottom. Each level is has_many with the one below it. In the Response''s form, I want to display Question.ask for each question in the Survey as a label for each Answer in the Response. Maybe there is a better way to go about doing this, but I''ve tried numerous approaches, refactoring the databases, and am just about to switch over to .NET (jk). -- 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.
Hassan Schroeder
2010-Jun-15 14:30 UTC
Re: Re: How do you access attributes in form templates?
On Tue, Jun 15, 2010 at 7:14 AM, Dee <dgerton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Explain how to display the sku and color in the form for each item?> <% f.fields_for @shelf.items do |item_fields| %> > <%# ****Want :sku here**** #%>Maybe I''m totally missing your point, but shouldn''t that just be, e.g. <% f.fields_for @shelf.items.each do |item| %> <%= item.sku %> ... ?? -- 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 for replying, Hassan. I don''t think you''re missing the point, and, yes, that''s what *I* think it should be too. The problem is "item" is type ActionView:Helpers:FormBuilder, and therefore doesn''t have a sku property. Or maybe /I''m/ missing something (I tried to make that clearer by naming it "item_fields" rather than just "item" as you''ve done here.) Dee On Jun 15, 10:30 am, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Jun 15, 2010 at 7:14 AM,Dee<dger...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Explain how to display the sku and color in the form for each item? > > <% f.fields_for @shelf.items do |item_fields| %> > > <%# ****Want :sku here**** #%> > > Maybe I''m totally missing your point, but shouldn''t that just be, e.g. > > <% f.fields_for @shelf.items.each do |item| %> > <%= item.sku %> > ... > ?? > -- > Hassan Schroeder ------------------------ hassan.schroe...-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.
Frederick Cheung
2010-Jun-15 16:34 UTC
Re: How do you access attributes in form templates?
On Jun 15, 5:05 pm, Dee <dger...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for replying, Hassan. > > I don''t think you''re missing the point, and, yes, that''s what *I* > think it should be too. The problem is "item" is type > ActionView:Helpers:FormBuilder, and therefore doesn''t have a sku > property. Or maybe /I''m/ missing something >if you''ve got a form builder then form_builder.object is the object it is bound to. Fred> (I tried to make that clearer by naming it "item_fields" rather than > just "item" as you''ve done here.) > > Dee > > On Jun 15, 10:30 am, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Tue, Jun 15, 2010 at 7:14 AM,Dee<dger...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Explain how to display the sku and color in the form for each item? > > > <% f.fields_for @shelf.items do |item_fields| %> > > > <%# ****Want :sku here**** #%> > > > Maybe I''m totally missing your point, but shouldn''t that just be, e.g. > > > <% f.fields_for @shelf.items.each do |item| %> > > <%= item.sku %> > > ... > > ?? > > -- > > Hassan Schroeder ------------------------ hassan.schroe...-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, that what I was looking for. Just found it in the source code. Works like a charm. Thanks Frederick. On Jun 15, 12:34 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 15, 5:05 pm, Dee <dger...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks for replying, Hassan. > > > I don''t think you''re missing the point, and, yes, that''s what *I* > > think it should be too. The problem is "item" is type > > ActionView:Helpers:FormBuilder, and therefore doesn''t have a sku > > property. Or maybe /I''m/ missing something > > if you''ve got a form builder then form_builder.object is the object it > is bound to. > > Fred > > > (I tried to make that clearer by naming it "item_fields" rather than > > just "item" as you''ve done here.) > > > Dee > > > On Jun 15, 10:30 am, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Tue, Jun 15, 2010 at 7:14 AM,Dee<dger...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Explain how to display the sku and color in the form for each item? > > > > <% f.fields_for @shelf.items do |item_fields| %> > > > > <%# ****Want :sku here**** #%> > > > > Maybe I''m totally missing your point, but shouldn''t that just be, e.g. > > > > <% f.fields_for @shelf.items.each do |item| %> > > > <%= item.sku %> > > > ... > > > ?? > > > -- > > > Hassan Schroeder ------------------------ hassan.schroe...-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.