danwick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-25 05:56 UTC
Multiple Nested RESTful Resources
(I''m very much a newbie, so forgive me if I''m leaving any details out.) I''m trying to understand how to relate 3 resources in a RESTful way. The scenario I''m playing around with is a survey model. A survey has many questions, each question has many choices. The 3 resources are Surveys, Questions, Choices. Example: Nature Survey (survey) -- http://localhost:3000/surveys/1 What color is the sky? (question) -- http://localhost:3000/surveys/1/questions/1 red (choice) -- http://localhost:3000/surveys/1/questions/1/choices/1 green (choice) -- http://localhost:3000/surveys/1/questions/1/choices/2 blue (choice) -- http://localhost:3000/surveys/1/questions/1/choices/3 Which season is the coldest? (question) -- http://localhost:3000/surveys/1/questions/2 summer (choice) -- http://localhost:3000/surveys/1/questions/2/choices/1 spring (choice) -- http://localhost:3000/surveys/1/questions/2/choices/1 fall (choice) -- http://localhost:3000/surveys/1/questions/2/choices/1 winter (choice) -- http://localhost:3000/surveys/1/questions/2/choices/1 I''ve set up the models and added the belongs_to and has_many relationships. I believe I have the routing set up correctly: ------------------------------------------------------------------ routes.rb map.resources :surveys do |survey| survey.resources :questions do |question| question.resources :choices end end ------------------------------------------------------------------ Where I am running into trouble is the Show view of a survey. I used the generate scaffold_resource, so I''m not doing anything beyond just displaying a chosen survey with all of the questions with their choices. ------------------------------------------------------------------ views/surveys/show.rhtml <p> <b>Title:</b> <%=h @survey.title %> </p> <p> <b>Description:</b> <%=h @survey.description %> </p> <p> <b>Created at:</b> <%=h @survey.created_at %> </p> <% unless @survey.questions.empty? %> <%= render :partial => "/questions/question/", :collection => @survey.questions %> <% unless @question.choices(params[@survey]).empty? %> <%= render :partial => "/choices/choice/", :collection => @question.choices %> <% end %> <%= link_to "Add choice", new_choice_url(@question.choices) %> <% end %> <%= link_to "Add question", new_question_url(@survey) %> | <%= link_to ''Edit'', edit_survey_path(@survey) %> | <%= link_to ''Back'', surveys_path %> ------------------------------------------------------------------ (The partials are set to display just the title column from the questions and choices columns.) I''m getting the following error when pulling up http://localhost:3000/surveys/1: "You have a nil object when you didn''t expect it! The error occurred while evaluating nil.choices" Any thoughts on where I''m going wrong? Thanks in advance and let me know if I''ve left out any pertinent info. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
danwick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-25 13:35 UTC
Multiple Nested RESTful Resources
I have a domain model with 3 resources: Surveys, Questions, Choices. (Surveys have_many Questions; Questions have_many Choices. Questions belong_to Survey; Choices belong_to Question) Here is how I''ve set up the routes: ------------------------------------------------------------ ActionController::Routing::Routes.draw do |map| map.resources :surveys do |survey| survey.resources :questions do |question| question.resources :choices end end ... ------------------------------------------------------------ I am getting an error when trying to display the relationship in the surveys/show.rhtml view: ------------------------------------------------------------ <p> <b>Title:</b> <%=h @survey.title %> </p> <p> <b>Description:</b> <%=h @survey.description %> </p> <p> <b>Created at:</b> <%=h @survey.created_at %> </p> # loop for displaying all questions for a survey <% unless @survey.questions.empty? %> <%= render :partial => "/questions/question/", :collection => @survey.questions %> # loop for displaying question choices <% unless @question.choices.empty? %> <%= render :partial => "/choices/choice/", :collection => @question.choices %> <% end %> <%= link_to "Add choice", new_choice_url(@question.choices) %> <% end %> (The partials are both just displaying a single column "title" from the questions and surveys table.) ------------------------------------------------------------ This is the error: "You have a nil object when you didn''t expect it! The error occurred while evaluating nil.choices" Any thoughts on what I''m doing wrong? I am very much a newbie, so if I''m missing any pertinent or supporting info, please let me know. Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If Survey has_many Questions, then you probably don''t have a @question variable in the Survey#show action, unless there is a default Question or I''m just misunderstanding something. Have you set @question? If not, then that''s your error. It seems like the following bit should go in your _question.rhtml partial, not the show.rhtml: <% unless @question.choices(params[@survey]).empty? %> <%= render :partial => "/choices/choice/", :collection => @ question.choices %> <% end %> and I believe you''ll need to use question instead of @question if inside a collection partial. ed On 1/25/07, danwick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <danwick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I have a domain model with 3 resources: Surveys, Questions, Choices. > (Surveys have_many Questions; Questions have_many Choices. Questions > belong_to Survey; Choices belong_to Question) > > Here is how I''ve set up the routes: > > ------------------------------------------------------------ > ActionController::Routing::Routes.draw do |map| > > map.resources :surveys do |survey| > survey.resources :questions do |question| > question.resources :choices > end > end > > ... > ------------------------------------------------------------ > > I am getting an error when trying to display the relationship in the > surveys/show.rhtml view: > > ------------------------------------------------------------ > <p> > <b>Title:</b> > <%=h @survey.title %> > </p> > > <p> > <b>Description:</b> > <%=h @survey.description %> > </p> > > <p> > <b>Created at:</b> > <%=h @survey.created_at %> > </p> > > # loop for displaying all questions for a survey > <% unless @survey.questions.empty? %> > <%= render :partial => "/questions/question/", :collection => @ > survey.questions %> > > # loop for displaying question choices > <% unless @question.choices.empty? %> > <%= render :partial => "/choices/choice/", :collection => @ > question.choices %> > <% end %> > > <%= link_to "Add choice", new_choice_url(@question.choices) %> > > <% end %> > > (The partials are both just displaying a single column "title" from the > questions and surveys table.) > ------------------------------------------------------------ > > This is the error: > "You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.choices" > > Any thoughts on what I''m doing wrong? > > I am very much a newbie, so if I''m missing any pertinent or supporting > info, please let me know. Thanks in advance. > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---