创建nested_attributes使before_create不起作用

时间:2015-10-11 23:38:45

标签: ruby-on-rails ruby

我试图在控制台中创建用户:

2.2.1 :012 > u.save!
   (0.2ms)  BEGIN
  User Exists (0.7ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('noc@co.co') LIMIT 1
  SQL (2.1ms)  INSERT INTO "users" ("id", "first_name", "last_name", "email", "title", "time_zone", "company_id", "password_digest", "created_at", "updated_at", "activation_digest") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["id", 200], ["first_name", "Random"], ["last_name", "Dude"], ["email", "noc@co.co"], ["title", "CEO"], ["time_zone", "Stockholm"], ["company_id", 1], ["password_digest", "$2a$10$bHHA/JP5IMrucGUXRWMpsO8sInaouSqn48M.fDHpjGdvedu3Napra"], ["created_at", "2015-10-11 23:09:38.213109"], ["updated_at", "2015-10-11 23:09:38.213109"], ["activation_digest", "$2a$10$WF3bUOtbC1gk4ZnX58cJZO5k7P7YV6wvhmwz7EErTdvIseNuy0oyq"]]
2.2.1 :013 > u.activation_token
 => "vKrs0jtvZRiyU-YVE-aPXw"

现在,当我尝试创建一个用户时,before_create不起作用,我尝试将其更改为before_validation,但也没有。

用户是公司的嵌套属性,在公司#new。

中创建
class User < ActiveRecord::Base
  belongs_to :company
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest

现在当我执行@ user.activation_token时,它返回nil。这是我在应用程序上尝试时的控制台日志:

Started POST "/companies" for ::1 at 2015-10-12 01:31:47 +0200
Processing by CompaniesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZTEtGR2Dd1FDDYm9j4/SiqSTP646R8gctFx4aJHM9QDP+RQky8SG6gkomLbf+E+LgMi+aah1YOhCkUsg3uSYoQ==", "company"=>{"time_zone"=>"Stockholm", "users_attributes"=>{"0"=>{"first_name"=>"swaga", "last_name"=>"swaga", "email"=>"swaga@wkoa.com", "password"=>"[FILTERED]"}}, "name"=>"swaga"}, "commit"=>"Create Company"}
   (0.2ms)  BEGIN
  User Exists (1.3ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('swaga@wkoa.com') LIMIT 1
  SQL (7.1ms)  INSERT INTO "companies" ("name", "time_zone", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "swaga"], ["time_zone", "Stockholm"], ["created_at", "2015-10-11 23:31:47.380530"], ["updated_at", "2015-10-11 23:31:47.380530"]]
  SQL (0.5ms)  INSERT INTO "users" ("first_name", "last_name", "email", "password_digest", "activation_digest", "company_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["first_name", "swaga"], ["last_name", "swaga"], ["email", "swaga@wkoa.com"], ["password_digest", "$2a$10$3GsPACTg5HPodiqdedsCvu52N64BEE3/fA77p.oBHAhM51zCSzUV."], ["activation_digest", "$2a$10$75y9jP.eZxmWI1KjHZrg3.DuB5GSwiS2UsaQdtV25ClBHDi.z4Pte"], ["company_id", 8], ["created_at", "2015-10-11 23:31:47.390462"], ["updated_at", "2015-10-11 23:31:47.390462"]]
   (6.7ms)  COMMIT
  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."company_id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["company_id", 8]]
  Rendered user_mailer/account_activation.html.erb within layouts/mailer (7.8ms)

UserMailer#account_activation: processed outbound mail in 29.2ms
Completed 500 Internal Server Error in 380ms (ActiveRecord: 16.9ms)

ActionController::UrlGenerationError - No route matches {:action=>"edit", :controller=>"account_activations", :email=>"swaga@wkoa.com", :id=>nil} missing required keys: [:id]:

现在我看到返回了activation_digest,所以我认为这个问题实际上只是before_create无法正常工作?这很奇怪,因为它们不应该能够创建一个激活摘要,因为两者是相连的:

# Creates and assigns the activation token and digest.
def create_activation_digest
  self.activation_token  = User.new_token
  self.activation_digest = User.digest(activation_token)
end

1 个答案:

答案 0 :(得分:0)

您的错误说:

ActionController::UrlGenerationError - No route matches {:action=>"edit", :controller=>"account_activations", :email=>"swaga@wkoa.com", :id=>nil} missing required keys: [:id]:

在您看来,您似乎在没有edit_account_activation_path参数的情况下致电id并且为您创建了问题。尝试在user.id调用中将edit_account_activation_path作为参数发送。

这样的事情:

edit_account_activation_path(user.email, user.id)

这应该可以解决你的问题。