【发布时间】:2016-10-19 20:25:08
【问题描述】:
当用户通过 devise_invitble 创建帐户时(发送初始邀请时),我被困住了,不确定如何向用户添加“account_id”。
基本的工作流程是所有者创建一个帐户,然后该帐户的所有者可以邀请某人使用该应用程序并设计邀请。我需要跟踪用户是否与帐户相关联,因为根据计划类型,帐户只有“x”个用户。
class InvitationsController < Devise::InvitationsController
after_action :update_user_account, :only => [:create]
def update_user_account
@user = User.find_by_email(params[:user][:email])
@user.update_attributes(:account_id => current_account.id )
end
end
这是我现在正在使用的,但是当我在 Rails 控制台中拉起该用户并在服务器输出中查看它时,用户 account_id 仍然为零。
这是帐户模型:
class Account < ApplicationRecord
include ImageUploader[:image]
# Constants
RESTRICTED_SUBDOMAINS = %w(www patrolvault admin test type taurenapplabs taurenmaterialservices)
# Before Actions
before_validation :downcase_subdomain
# Relationships
belongs_to :owner, class_name: 'User', optional: true
accepts_nested_attributes_for :owner
has_many :users
# Validations
validates :owner, presence: true
validates :subdomain, presence: true,
uniqueness: { case_sensitive: false },
format: { with: /\A[\w\-]+\Z/i, message: 'Contains invalid characters.' },
exclusion: { in: RESTRICTED_SUBDOMAINS, message: 'Restricted domain name'}
has_one :plan
accepts_nested_attributes_for :plan
private
def downcase_subdomain
self.subdomain = self.subdomain.downcase
end
end
这是用户模型:
class User < ApplicationRecord
# Constants & Enums
USER_LIMITS = ActiveSupport::HashWithIndifferentAccess.new(
#Plan Name #Auth Users
responder: 6,
first_responder: 12,
patrol_pro: 30,
guardian: 60
)
# Before Actions
# Devise Modules
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :invitable, :lockable, :timeoutable
# Relationships
belongs_to :account, optional: true
# Validations
validates :f_name, presence: true
validates :l_name, presence: true
validates :date_of_birth, presence: true
#validate :must_be_below_user_limit
# Custom Methods
def full_name
l_name.upcase + ", " + f_name
end
end
拜托,非常感谢这里的任何帮助!,这真的让我很困惑。
【问题讨论】:
-
听起来用户记录可能没有保存,因为验证没有通过。您确定用户有 f_name、l_name 和 date_of_birth 吗?
-
@infused 当我发送邀请时,控制台中唯一填写的字段是电子邮件和所有可邀请字段,当用户接受邀请时,他们会看到一个允许他们添加的字段他们的姓名和密码,所有这些都通过并出现在控制台和日志中……唯一不更新的项目是 account_id。 -- 生病添加上面的控制台读数。
-
这是因为
@user.update_attributes(:account_id => current_account.id )因为验证错误而失败。 -
@infused,那我怎么才能通过呢?或者只是删除验证?
标签: ruby-on-rails ruby-on-rails-4 devise-invitable