(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|
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
Title: <%=h @survey.title %>
Description: <%=h @survey.description %>
Created at: <%=h @survey.created_at %>
<% unless @survey.questions.empty? %>
<%= render :partial => “/questions/question/”, :collection =>
<% unless @question.choices(params[@survey]).empty? %> <%= render :partial => "/choices/choice/", :collection =>
<% 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.