在用户模型中before_create - 基于电子邮件(RAILS)设置整数 [英] before_create in user model -- set integer based on email (RAILS)

查看:108
本文介绍了在用户模型中before_create - 基于电子邮件(RAILS)设置整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据他们的电子邮件域将用户分配给他们的公司组。我正在使用devise +确认,所以我避免使用正则表达式(不需要验证它的一个有效的电子邮件...),我试图以一种简单的方式来做到这一点。所以本质上,它将强制用户company_id(与该表匹配)在注册时被分配,然后如果他们的公司不存在则不允许他们注册。所以这将适用于test@company.com和test@recruiting.company.com

I'm trying to assign a user to their companys group based on their email domain. I'm using devise + confirmation, so I avoided using regex (dont need to validate that its a valid email...), and am trying to do this in a simple way. So essentially, it would force the users company_id (which matches up with that table) to be assigned in sign up, and then not allow them to sign up if their company doesnt exist. So this will work for both test@company.com and test@recruiting.company.com

在用户模型中

before_create :company_placement

...

def company_placement
  user_domain = (:email).split('@').last

  while user_domain.split('.').count > 2
    user_domain = user_domain.split('.', 2).last
  end

  if Company.find_by_domain(user_domain) != nil
    (:company_id) = Company.find_by_domain(user_domain).id
  else
    #error out
  end
end

当我在rails控制台中逐步执行此操作时,似乎可以正常工作。但是在我运行的控制台中,

When I do this in rails console step by step, it seems to work. But in console when i run,

> user = User.create!(name: "test", email: "test@example.com", password: "foobar")

我得到未定义的局部变量或方法'user'for#&''User ....

I get undefined local variable or method 'user' for #<'User....

感谢任何帮助,仍然学习

Thanks for any help, still learning rails...

推荐答案

所以我再玩一些,认为我找到了一个我喜欢的解决方案

So I played with this some more and think i found a solution I like

用户模型

before_validation :company_placement

...

def company_placement
  user_domain = self.email.split('@').last

  while user_domain.split('.').count > 2
    user_domain = user_domain.split('.', 2).last
  end

  if Company.find_by_domain(user_domain) != nil
    self.company_id = Company.find_by_domain(user_domain).id
  end
end

创建设计注册控制器 - 控制器/注册 _ controller.rb

新注册控制器

class RegistrationsController < Devise::RegistrationsController
  before_filter :verify_company, only: :create

  private

    def verify_company
      build resource #necessary for devise

      user_domain = resource.email.split('@').last

      while user_domain.split('.').count > 2
        user_domain = user_domain.split('.', 2).last
      end

      unless Company.find_by_domain(user_domain) != nil
        flash[:error] = 'Sorry, your company does not exist yet'
        redirect_to root_path
      end
    end
end

routes.rb

devise_for :users, :controllers => { :registrations => "registrations" }

所以确定有一个更优雅的解决方案,但这对我有用。处理控制器中的错误/闪存,然后如果公司存在,用户将通过模型自动分配给公司。

So im sure there is a more elegant solution, but this works for me. Handles the errors/flash in the controller, and then if the company exists, it the user gets auto assigned to the company through the model.

这篇关于在用户模型中before_create - 基于电子邮件(RAILS)设置整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆