由于某种原因,活动记录不会保存

时间:2013-04-02 05:17:16

标签: ruby-on-rails

我有一个名为assignment的模型,它代表用户与多选测验应用中的问题之间的丰富连接。当用户回答问题时,响应将记录在分配控制器

中的以下更新方法中

    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

我有一个被称为等级的保存被调用。这是模型:

 

    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

因此,当我第一次提交回复时,保存工作完美,并相应地重定向。之后,如果我再次尝试编辑问题并重新提交表单,则保存失败。

有人会想到这可能发生的原因吗?

另外我知道第二次重定向有错误,所以如果有人可以纠正我的用法,这将是一个额外的帮助

编辑这是编辑erb,以防有人在这里发现错误。我还在上面的控制器中添加了编辑方法。

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

编辑2我刚刚在rails控制台中手动完成了这些步骤,没有任何问题,因此必须有一些奇怪的事情发生。

2 个答案:

答案 0 :(得分:1)

这可能是因为您的grade()回调方法返回false,这将取消所有操作(如documentation中所述)。

答案 1 :(得分:0)

希望这对你有用。

分配模型中,将correct字段添加为attr_accessible。第一次response为nil时,它不会在before_save方法中执行语句,因此您的代码将是

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