I have an model called assignment, which represents a rich join between users and questions in a multiple choice quiz app. when a user answers a question, the response is recorded in the following update method in the assignments controller
def edit
@assignment = Assignment.find_by_id(params[:id])
@user = @assignment.user
@question = @assignment.question
puts "user #{@user} question #{@question} assignment #{@assignment}"
end
def update
@assignment = Assignment.find(params[:id])
@user = @assignment.user
@assignment.update_attributes(:response => params[:assignment][:response])
if @assignment.save
flash[:notice] = "Your response has been saved"
redirect_to(:action => 'show', :id => @assignment.id , :user_id => @user.id)
else
puts @assignment.save
puts "could not save"
render(user_assignment_path(@user, @assignment) , :html => {:method => :get})
end
end
I have a before save called grade that gets called. Here is the model:
class Assignment ActiveRecord::Base
belongs_to :user
belongs_to :question
attr_accessible :title, :body, :user_id, :question_id , :response
before_save :grade
def grade
self.correct = (response == self.question.solution) unless response == nil
end
end
So the first time I submit a response, the save works perfectly and it redirects me accordingly. after that, if i try to edit the question again and resubmit the form, the save fails.
Can anyone think of a reason this might be occurring?
In addition I know there is an error in the second redirect, so if someone could correct my usage that would be a bonus help
EDIT Here is the Edit erb, in case someone can spot something wrong here. I also added the edit method in the controller above.
<div class="admin-display">
<%if @admin%>
<p>
You may edit the user's response below or change the question to override whether the question is marked correct.
</p>
<%end%>
</div>
<div class="question body">
<%= @question.question %>
</div>
<div class="answer-choices">
<%= params[:user_id] + " " + params[:id] %>
<ol type="A">
<%=form_for(@assignment , :url => user_assignment_path(params[:user_id] , params[:id]) , :html => {:method => "put"}, :user_id => params[:user_id]) do |f|%>
<%[@question.answerA, @question.answerB ,@question.answerC ,@question.answerD].each do |choice|%>
<li>
<%= f.radio_button(:response, choice)%>
<%= choice %>
</li>
<%end%>
</ol>
<div class="form-buttons">
<%= submit_tag("Submit Response") %>
</div>
<%end%>
</div>
EDIT 2 I have just gone through the steps by hand in the rails console, without any issue, so there must be something strange going on.