0

I have 2 models, Assessments and Questions. Assessments have many questions.

In routes, I have:

map.resources :assessments, :has_many => :questions
map.root :assessments

I checked rake routes, it's as expected

On the form to create a new question, I get the following error:

undefined method `questions_path' for #<ActionView::Base:0x6d3cdb8>

If I take out the form, the view loads fine, so I think it's something with the code in this view - I'm getting the error on the form_for line:

<h1>New question</h1>

<% form_for [@assessment, @question] do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :content %><br />
    <%= f.text_field :content %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Cancel', assessment_path(@assessment) %>

Rake Routes - http://pastebin.com/6fKUPTjq

Code to question controller - http://pastebin.com/URzpmEcg

Code to assessment controller - http://pastebin.com/HstvFTq4

Can anyone help me debug it? Thanks!

stringo0
  • 2,720
  • 7
  • 38
  • 52
  • The correct method should be assessment_questions_path, I think. Not sure why questions_path is being called though. – alternative Jun 09 '10 at 23:03
  • Yeah - assessment_questions_path is what I think it should call, but I'm not sure why it isn't. – stringo0 Jun 09 '10 at 23:07
  • Could you pastebin the code for the controller? – alternative Jun 09 '10 at 23:10
  • done - I added the controller code. – stringo0 Jun 09 '10 at 23:13
  • I'm not completely sure, but do you know if rails defines `@assessment` for you automatically in the case of the nested routes? It might be nil. – alternative Jun 09 '10 at 23:20
  • Not sure - looking into that question. – stringo0 Jun 09 '10 at 23:24
  • I think that may be the case and it is trying to fall back onto shallow routing, which you haven't enabled, thus causing the error. Although it really should report the nil value as an error instead in that case. – alternative Jun 09 '10 at 23:27
  • I get to the view with this link - <%= link_to 'New question', new_assessment_question_path(@assessment) %> So the value shouldn't be nil. – stringo0 Jun 09 '10 at 23:27
  • I'm not completely sure, but I think that may only set assessment_id, not assessment. Try adding "@assessment = Assessment.find(params[:assessment_id])" to the controller. – alternative Jun 09 '10 at 23:31
  • Adding that lets me see the new question page, but if I hit submit, I get a "NoMethodError in Questions#create" - but it sure is progress! – stringo0 Jun 09 '10 at 23:37
  • Oh, and the params hash if you have it available through debugging. – alternative Jun 09 '10 at 23:40
  • That error dissapeared after adding "@assessment = Assessment.find(params[:assessment_id])" to the create function as well - I'm gonna try to debug this by myself for a bit - I'll post back in 20 minutes. Thanks for all the help! – stringo0 Jun 09 '10 at 23:42
  • I don't see how that helped... You never use @assessment. I was going to suggest to use "@assessment.questions.new" instead of "Question.new" after defining @assessment. – alternative Jun 09 '10 at 23:44
  • Actually, I see how that would help only if it doesn't pass validations. – alternative Jun 09 '10 at 23:49
  • Hmm - thanks for the help! I'll be able to refactor the code later, but the "@assessment = Assessment.find(params[:assessment_id])" tip was killer! How do I give you points for that? Maybe just post that tip, and I'll check it off as the answer - cause that did make the bug go away. – stringo0 Jun 09 '10 at 23:59

1 Answers1

1

You need to have

@assessment = Assessment.find(params[:assessment_id])

in the controller. Otherwise, @assessment is nil.

alternative
  • 12,703
  • 5
  • 41
  • 41