【问题标题】:Rails 4 before_create method not saving to DBRails 4 before_create 方法不保存到数据库
【发布时间】:2015-12-09 05:24:57
【问题描述】:

每次将 Post 请求发送到 URLwww.example.com/emailsig.json 时,我都会尝试为 SQL DB 创建一个新的散列令牌。我正在使用 before_create 运行一个函数,该函数正在被调用,但在函数中创建的变量没有保存到数据库中。我是 Rails 新手,真的不知道从哪里开始解决这个问题。我认为这可能与强参数有关?

轨道模型

class Distribution < ActiveRecord::Base
  belongs_to :user
  #validates :distribution_digest, presence: true
  attr_accessor :distribution_token, :distribution_digest
  before_create :create_distribution_digest
  validates :signature_id, presence: true

  def Distribution.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def Distribution.new_token
    SecureRandom.urlsafe_base64
  end

  private

    # Creates and assigns the distribution token and digest.
    # Functions create tokens and hashed tokens as expected.
    def create_distribution_digest
      self.distribution_token  = Distribution.new_token
      self.distribution_digest = Distribution.digest(distribution_token)
    end
end

Rails 控制器

class DistributionsController < ApplicationController
  before_action :logged_in_user, only: [:email_sig]

  def create
    if current_user.microposts.find(params[:signature_id])
      @distribution = current_user.distributions.build(distribution_params)
      @distribution.save
      respond_with current_user.microposts.find(params[:signature_id])
    end
  end

  private

    def distribution_params
      params.require(:distribution).permit(:signature_id).merge(:distributed_at => Time.zone.now)
    end

end

服务器日志

Started POST "/emailSig.json" for 180.181.247.76 at 2015-12-09 05:01:47 +0000
Processing by DistributionsController#create as JSON
  Parameters: {"signature_id"=>8, "distribution"=>{"signature_id"=>8}}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  Micropost Load (3.2ms)  SELECT  "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? AND "microposts"."id" = ?  ORDER BY "microposts"."created_at" DESC LIMIT 1  [["user_id", 2], ["id", 8]]
   (0.2ms)  begin transaction
blah
pW00Dn7L9p7CnC6BysvImQ
  SQL (1.0ms)  INSERT INTO "distributions" ("signature_id", "distributed_at", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["signature_id", "8"], ["distributed_at", "2015-12-09 05:01:48.469044"], ["user_id", 2], ["created_at", "2015-12-09 05:01:48.484218"], ["updated_at", "2015-12-09 05:01:48.484218"]]
   (11.8ms)  commit transaction

控制台查询(将 distribution_digest 显示为 nil)

  Distribution.last
  Distribution Load (0.4ms)  SELECT  "distributions".* FROM "distributions"  ORDER BY "distributions"."id" DESC LIMIT 1
=> #<Distribution id: 27, user_id: 2, signature_id: "8", distribution_digest: nil, distributed_at: "2015-12-09 05:01:48", created_at: "2015-12-09 05:01:48", updated_at: "2015-12-09 05:01:48">

【问题讨论】:

    标签: ruby-on-rails-4 strong-parameters before-save


    【解决方案1】:

    似乎列名中的保留字digest 会导致此问题。我尝试将此列重命名为 sth_else 并且一切正常,但是使用 distribution_digest 我什至无法在 rails 控制台中添加此属性。尝试重命名此列,它应该开始工作:)

    【讨论】:

    • 感谢您的回复,但我只是重命名了所有内容,并没有任何区别。此外,Michael Hartl 教程中以几乎完全相同的方式使用摘要一词。我能看到的唯一区别是我试图在属于用户的模型上使用它,而不是用户模型本身。
    • 我将变量名从 distribution_digest 更改为 signaturekey_hash 并遇到了同样的问题。然后将其更改为 ekeycode 并且它起作用了。所以毕竟是变量名......我以后如何避免这种事情,看起来我碰到了2个我不应该使用的变量名,但这看起来它将是一个恒定的来源头痛!
    • 我尽量不使用保留字 - 这里有一个包含所有这些字的列表 - rubymagic.org/posts/ruby-and-rails-reserved-words
    【解决方案2】:

    我通过从 attr_accessor 中删除 :distribution_digest 来完成这项工作

    attr_accessor 是一个 ruby​​ 代码,用于(快速)在类中创建 setter 和 getter 方法。就是这样。

    现在,缺少的解释是,当您以某种方式在(Rails)模型与数据库表之间创建链接时,您永远,永远,永远不需要模型中的attr_accessor 来按顺序创建 setter 和 getter以便能够修改表的记录。

    https://stackoverflow.com/a/12938809

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多