Forum: Ruby on Rails Multiple Nested RESTful Resources

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
49d0c6dc5ab078e132c7998b311ea783?d=identicon&s=25 (Guest)
on 2007-01-27 02:35
(Received via mailing list)
(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.


Nature Survey (survey) -- http://localhost:3000/surveys/1

What color is the sky? (question) --
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) --
summer (choice) --
spring (choice) --
fall (choice) -- http://localhost:3000/surveys/1/questions/2/choices/1
winter (choice) --

I've set up the models and added the belongs_to and has_many
relationships.  I believe I have the routing set up correctly:


  map.resources :surveys do |survey|
    survey.resources :questions do |question|
      question.resources :choices

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


  <%=h @survey.title %>

  <%=h @survey.description %>

  <b>Created at:</b>
  <%=h @survey.created_at %>

<% 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

"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.
This topic is locked and can not be replied to.