2

控制器/makenew.rb

class MakeController < Controller

  map '/makenew'
  #require 'model/debate'

  def debate
    if request.post? #this line is potentially dangerous!
      #---> 1/3 fetch postdata
      data = request.subset(:question, :type, :category, :assertion)
      data['user_id'] = user.id #id = request.params['id']
      #---> 2/3 check permissions
      if user.points < 40
        flash[:error] = 'You don\'t have enough points to make a debate.'
        redirect_referrer
      else
        debate = Debate.new
      end
      #---> 3/3 modify database
      begin
        debate.save(data)
        flash[:success] = success
        flash[:form_data] = debate
        redirect 'debates'
      rescue => e
        Ramaze::Log.error(e)
        #flash[:form_errors]   = debate.errors
        #flash[:error] = data
        flash[:error] = e
        #flash[:error] = 'Failure whilst saving. Contact technical support!'
        redirect 'debates' #redirect_referrer
      end
     #| 
    end #closes posting conditional
  end #closes makesave 
end

我得到的错误是。

SQLite3::ConstraintException:debates.question 可能不是 NULL

我检查了postdata data.question,它不为空。

到底是怎么回事?

4

2 回答 2

1

Are you sure that your model accepts mass assignment of primary keys?

Try calling Debate.unrestrict_primary_key

You can check the rules in the Sequel documentation.

于 2012-09-11T21:17:09.657 回答
1

您需要将“数据”传递给#update. 因此:

debate.save(data)

错了,你必须这样做:

debate.update(data)
debate.save

如果您不这样做,则您的辩论对象没有分配成员,因此其问题成员为零,违反了您的数据库约束。

在此处查看#save 和#update 之间的区别:

简而言之:#save将当前模型实例保存到数据库中,同时#update将在一次操作中更改一堆实例属性。

但是您必须记住,更改模型实例的属性不会将它们写入数据库。你总是必须#save明确地调用。

于 2012-09-13T05:26:20.960 回答