2013-04-02 3 views
0

객관식 퀴즈 앱에서 사용자와 질문 간의 풍부한 결합을 나타내는 할당이라는 모델이 있습니다. 사용자가 질문에 응답 할 때, 응답 내가 호출되는 전에 호출을 막아 등급을 할당 컨트롤러어떤 이유로 든 활성 레코드가 저장되지 않음

 

    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 

에서 다음 업데이트 방법에 기록됩니다. 여기에 모델이 있습니다 :

처음으로 응답을 제출하면 저장이 완벽하게 작동하고 이에 따라 적절하게 리디렉션됩니다. 그 후에 질문을 다시 편집하고 양식을 다시 제출하려고하면 저장이 실패합니다.

누구나 이런 일이 발생할 수 있다고 생각할 수 있습니까?

는 또한 누군가가 경우 누군가가 여기에 잘못을 발견 할 수 있습니다 여기에

편집 편집 ERB있는 보너스 도움을 될 것입니다 내 사용을 정정 할 수 그래서 만약 오류가, 두 번째 리디렉션에가 알고있다. 또한 위의 컨트롤러에서 edit 메서드를 추가했습니다.

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

+0

오류를 게시하십시오. – Jesper

+0

컨트롤러에서 학년 인스턴스 메서드를 호출하는 대신 콜백을 추가하는 것이 좋을 것이라고 생각합니다. 즉, 콜백을 저장할 때 컨트롤러의 메서드 만 호출한다는 것을 의미합니다. 콜백 스택을 사용하면 두통이 줄어 듭니다. 로딩 시간이 증가합니다. – dennis

+0

@Jesper 안녕하세요, 오류가 없습니다. 이것은 이상한 일입니다. 할당은 저장에 실패하고 else 논리를 따릅니다. 나는 콘솔에 인쇄 오류를 시도했지만 아무 것도 나왔다. – bluedevil

답변

0

Hope this will work for you.

In Assignment model, add correct 필드는 attr_accessible입니다. response로 첫 번째 시간은, 따라서 코드가

class Assignment ActiveRecord::Base 
    belongs_to :user 
    belongs_to :question 
    attr_accessible :title, :body, :user_id, :question_id , :response, :correct 
    before_save :grade 


    def grade 
    correct = (response == question.solution) unless response.nil? 
    end 
end 

및 cotroller 조치가 그것 때문에 그 사실에 아마

def update 
    @assignment = Assignment.find(params[:id]) 
    @user = @assignment.user 
    @assignment.update_attributes(:response => params[:assignment][:response]) 
    if @assignment.valid? 
    flash[:notice] = "Your response has been saved" 
    redirect_to(:action => 'show', :id => @assignment.id , :user_id => @user.id) 
    else 
    render(user_assignment_path(@user, @assignment) , :html => {:method => :get}) 
    end 
end 
+0

안녕하세요, 응답 해 주셔서 감사합니다. 이것은 불행히도 문제를 해결하지 못했습니다. 과제는 여전히 저장되지 않습니다. 응답과 수정이 무의미하지만 질문에 답변 한 후에 처음으로 작동한다는 것은 다소 당황 스럽습니다. – bluedevil

1

이 될 수있을 것입니다 before_save 방법으로 문을 실행하지 않는, 전무 당신의 grade() 콜백 메소드가 false를 반환하면 액션이 모두 취소됩니다 (documentation에 명시).

관련 문제