活动模型在 new 和 create 方法调用中插入 NULL 值

发布于 2024-12-29 17:56:06 字数 4256 浏览 2 评论 0原文

这是我在笔记模型的 create_note 方法调用上的 webrick 转储

Parameters: {"utf8"=>"✓", "authenticity_token"=>"T/F/oZaUYHz7G3HUjVKDs+Qjx+hrg6VqU4t1vr14ACc=", "note"=>{"notename"=>"Hello World 3", "notecontent"=>"3rd time hello world"}, "commit"=>"Create"}
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `notes` (`created_at`, `notecontent`, `notename`, `updated_at`, `user_id`) VALUES ('2012-01-30 07:04:31', NULL, NULL, '2012-01-30 07:04:31', 1)
   (3.3ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
  Rendered note/create.html.erb within layouts/application (2.5ms)
Completed 200 OK in 58ms (Views: 13.4ms | ActiveRecord: 5.8ms)

看到插入的 notecontent 带有 NULL 值,并且 注释名称。

我的模型文件是

class Note < ActiveRecord::Base
        self.table_name = "notes"
        validates_length_of :notename, :within => 5..50, :message => "Notename 5-50 chars"
        validates_length_of :notecontent, :within => 5..50, :message => "Notecontent 5-50 chars"
        attr_protected :id
        belongs_to :user

        attr_accessor :notename, :notecontent, :created_at, :updated_at

        def self.get_notes_by_user(id)
                usernotes =  Note.find :all, :conditions => ["user_id = ?", id]
                return usernotes
        end


        def self.create_note(name, content, user)
                n = Note.create(:notename => name , :notecontent => content, :user_id => user)
                if n.save!
                        return true
                else
                        return false
                end
        end

        def self.update_note (id, content)
                n = Note.find :first, :conditions => ["id = ?", id]
                n.notecontent = content;
                n.updated_at = 0.hour.from_now
        end
end

我的控制器文件,

class NoteController < ApplicationController

        #before_filter :login_required
  def create
        if session[:user] == nil
                redirect_to :controller => "user", :action => "login"
        end
        if request.post?
                @nname = params[:note][:notename]
                @ncontent = params[:note][:notecontent]
                @uid = session[:user].getid
                @ins = params.inspect
                status = Note.create_note @nname, @ncontent, @uid
                if status == false
                        flash[:warning] = "Error While Creating Note"
                else
                        flash[:notice] =  "Note Successfully Created"
                end
        end
  end

  def delete
  end

  def show
  end

  def index
        if session[:user] == nil
                redirect_to :controller => "user", :action => "login"
        end
        user = session[:user]
        @uname = user.getname
        #nn = Note.new
        @user_notes =  Note.get_notes_by_user(user.getid)
  end
end

我已检查是否正在为注释模型中的 create_note 方法中的参数填充值。但不知何故,它似​​乎无法将这些值映射到数据库查询语言中。请帮忙。

我的迁移文件是

class AddUidToNotes < ActiveRecord::Migration
  def self.up
        add_column :notes, :user_id, :int
  end
end

class CreateNotes < ActiveRecord::Migration
  def self.up
    create_table :notes do |t|
        t.column :notename, :string
        t.column :notecontent, :string
      t.timestamps
    end
  end

  def self.down
    drop_table :notes
  end
end

DBCONSOLE

mysql> desc notes;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| notename    | varchar(255) | YES  |     | NULL    |                |
| notecontent | varchar(255) | YES  |     | NULL    |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
| user_id     | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

This is my webrick dump on create_note method call of note model

Parameters: {"utf8"=>"✓", "authenticity_token"=>"T/F/oZaUYHz7G3HUjVKDs+Qjx+hrg6VqU4t1vr14ACc=", "note"=>{"notename"=>"Hello World 3", "notecontent"=>"3rd time hello world"}, "commit"=>"Create"}
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `notes` (`created_at`, `notecontent`, `notename`, `updated_at`, `user_id`) VALUES ('2012-01-30 07:04:31', NULL, NULL, '2012-01-30 07:04:31', 1)
   (3.3ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
  Rendered note/create.html.erb within layouts/application (2.5ms)
Completed 200 OK in 58ms (Views: 13.4ms | ActiveRecord: 5.8ms)

See that the insert coming with NULL values for notecontent and
notename.

My model file is

class Note < ActiveRecord::Base
        self.table_name = "notes"
        validates_length_of :notename, :within => 5..50, :message => "Notename 5-50 chars"
        validates_length_of :notecontent, :within => 5..50, :message => "Notecontent 5-50 chars"
        attr_protected :id
        belongs_to :user

        attr_accessor :notename, :notecontent, :created_at, :updated_at

        def self.get_notes_by_user(id)
                usernotes =  Note.find :all, :conditions => ["user_id = ?", id]
                return usernotes
        end


        def self.create_note(name, content, user)
                n = Note.create(:notename => name , :notecontent => content, :user_id => user)
                if n.save!
                        return true
                else
                        return false
                end
        end

        def self.update_note (id, content)
                n = Note.find :first, :conditions => ["id = ?", id]
                n.notecontent = content;
                n.updated_at = 0.hour.from_now
        end
end

My Controller file is

class NoteController < ApplicationController

        #before_filter :login_required
  def create
        if session[:user] == nil
                redirect_to :controller => "user", :action => "login"
        end
        if request.post?
                @nname = params[:note][:notename]
                @ncontent = params[:note][:notecontent]
                @uid = session[:user].getid
                @ins = params.inspect
                status = Note.create_note @nname, @ncontent, @uid
                if status == false
                        flash[:warning] = "Error While Creating Note"
                else
                        flash[:notice] =  "Note Successfully Created"
                end
        end
  end

  def delete
  end

  def show
  end

  def index
        if session[:user] == nil
                redirect_to :controller => "user", :action => "login"
        end
        user = session[:user]
        @uname = user.getname
        #nn = Note.new
        @user_notes =  Note.get_notes_by_user(user.getid)
  end
end

I have checked that the values are being populated for the arguments in the create_note method in the Note Model. but it seems somehow it is unable to map those values into the database query language. Please help.

My Migration file is

class AddUidToNotes < ActiveRecord::Migration
  def self.up
        add_column :notes, :user_id, :int
  end
end

class CreateNotes < ActiveRecord::Migration
  def self.up
    create_table :notes do |t|
        t.column :notename, :string
        t.column :notecontent, :string
      t.timestamps
    end
  end

  def self.down
    drop_table :notes
  end
end

DBCONSOLE

mysql> desc notes;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| notename    | varchar(255) | YES  |     | NULL    |                |
| notecontent | varchar(255) | YES  |     | NULL    |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
| user_id     | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2025-01-05 17:56:06

切勿将 attr_accessor 与数据库支持的属性一起使用。这将覆盖 ActiveRecord 提供的一组单独的访问器,这些访问器不会写入 ActiveRecord 使用的存储。

不要与 attr_accessible 混淆,它是批量分配保护系统的一部分,绝对可以使用

Don't ever use attr_accessor with a database backed attribute. This will override the accessors ActiveRecord provides with a separate set of accessors that don't write to the storage ActiveRecord uses.

Not to be confused with attr_accessible which is part of the mass assignment protection system and absolutely fine to use

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文