객관식 퀴즈 앱에서 사용자와 질문 간의 풍부한 결합을 나타내는 할당이라는 모델이 있습니다. 사용자가 질문에 응답 할 때, 응답 내가 호출되는 전에 호출을 막아 등급을 할당 컨트롤러어떤 이유로 든 활성 레코드가 저장되지 않음
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.
오류를 게시하십시오. – Jesper
컨트롤러에서 학년 인스턴스 메서드를 호출하는 대신 콜백을 추가하는 것이 좋을 것이라고 생각합니다. 즉, 콜백을 저장할 때 컨트롤러의 메서드 만 호출한다는 것을 의미합니다. 콜백 스택을 사용하면 두통이 줄어 듭니다. 로딩 시간이 증가합니다. – dennis
@Jesper 안녕하세요, 오류가 없습니다. 이것은 이상한 일입니다. 할당은 저장에 실패하고 else 논리를 따릅니다. 나는 콘솔에 인쇄 오류를 시도했지만 아무 것도 나왔다. – bluedevil