设计控制器中的创建用户(模型)在哪里

时间:2016-09-25 17:12:39

标签: ruby-on-rails ruby devise

我想在创建的用户中添加一个序列号,就像我在常规脚手架生成中一样,将一个序列号添加到一个名为" code"

的字段中
 def create
    @tester = Tester.new(tester_params)
    @tester.code =  @tester.created_at.to_s + ":" + rand(1235).to_s + ":" +      rand(5123).to_s + ":" + rand(1523).to_s + ":" + SecureRandom.base64
    respond_to do |format|
      if @tester.save
        format.html { redirect_to @tester, notice: 'Tester was successfully created.' }
        format.json { render :show, status: :created, location: @tester }
      else
        format.html { render :new }
        format.json { render json: @tester.errors, status: :unprocessable_entity }
      end
    end
  end

我生成了一个设计用户并在设计控制器代码中复制以覆盖设计会话处理程序。所以我认为我用设计自己的代码覆盖了代码,但我无法找到用户的创建位置,我一直在寻找设计注册码:

def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
  if resource.active_for_authentication?
    set_flash_message! :notice, :signed_up
    sign_up(resource_name, resource)
    respond_with resource, location: after_sign_up_path_for(resource)
  else
    set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
    expire_data_after_sign_in!
    respond_with resource, location: after_inactive_sign_up_path_for(resource)
  end
else
  clean_up_passwords resource
  set_minimum_password_length
  respond_with resource
end

我添加了_code_to_users,现在我想做一个 current_user.code = secureRandom.base64或其他什么

2 个答案:

答案 0 :(得分:2)

resource(用户)是在build_resource(sign_up_params)

中创建的

然而,Devise中的每个动作通常都会产生资源。

yield resource if block_given?

这使得在调用super时通过传递块非常容易扩展。

class TesterRegistrationContoller < Devise::RegistrationsController
  # ...
  def create
    super do |resource|
      # This runs before `.save` is called on the user.
      begin
        # do you really need all that other stuff?
        code = SecureRandom.uuid
      end while Tester.exists?(code: code)
      resource.code = code
    end
  end
end

答案 1 :(得分:0)

_ = self.navigationController?.popToViewController...