每次将Post请求发送到URLwww.example.com/emailsig.json时,我都会尝试为SQL DB创建一个新的哈希标记。我正在使用before_create来运行一个被调用的函数,但是函数中创建的变量没有保存到DB中。我是铁杆的新手,并不知道从哪里开始解决这个问题。我认为这可能与强参数有关吗?
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">
答案 0 :(得分:0)
列名中的保留字digest
似乎会导致此问题。我尝试将此列重命名为sth_else
,一切正常,但distribution_digest
我甚至无法在rails控制台中添加此属性。尝试重命名此列,它应该开始工作:)
答案 1 :(得分:0)
我从:distribution_digest
attr_accessor
,从而实现了这一目标
attr_accessor
是一个ruby代码,用于(快速)在Class中创建setter和getter方法。就是这样。现在,作为解释的缺失是,当您以某种方式创建(Rails)模型与数据库表之间的链接时,您永远不会在模型中需要
attr_accessor
来创建setter和getters为了能够修改表格的记录。