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

Title: <%=h @survey.title %>

Description: <%=h @survey.description %>

Created at: <%=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
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.