简体   繁体   English

Rails Devise sign_up操作的自定义字段

[英]Custom field for Rails Devise sign_up action

This is a common question on web, specially here on stack overflow... I know... 这是网上的一个常见问题,特别是在堆栈溢出的地方...我知道...
anyway I need to perform a little variation on this common rails newbie issue. 无论如何,我需要对该通用Rails新手问题进行一些改动。

I have a little bit complicated situation, then I'm going to semplify it. 我有一些复杂的情况,然后我将其简化。

On my user model I got all base Devise field, and some custom filed. 在我的用户模型上,我得到了所有基本的Devise字段,并且有一些自定义字段。 to manage this custom field on sing_up action I override Registration Controller as the official guide say, and then I add the sign_up_params function as this: 要在sing_up操作上管理此自定义字段,我按照官方指南的要求覆盖了注册控制器,然后添加了sign_up_params函数,如下所示:

    def sign_up_params
        params.require(:user).permit(:name, :surname, :username, :birthdate, :email, :password, :password_confirmation)
    end

Ok, untill here everything it's going well. 好的,直到一切顺利。

Now I have one more field, that is a reference to another user. 现在,我还有一个字段,它是对另一个用户的引用。 One User belong to a parent, and I need to calculate the parent on the server side (should not do this on form, "input type hidden" could create a security issue). 一个用户属于父级,我需要在服务器端计算父级(不应该在表单上执行此操作,“输入类型隐藏”可能会造成安全问题)。

Here the questions: 这里的问题:

1- Where's the best place to calculate the parent? 1-计算父母的最佳位置在哪里? on the sign_up_params function like this? 像这样在sign_up_params函数上?

    def sign_up_params
        parent = do_some_stuff_to_retrieve_parent(params[:some_token])
        params.require(:user).permit(:name, :surname, :username, :birthdate, :email, :password, :password_confirmation)
    end

2- When I'll get the parent, how I can put it into the new User object? 2-当我得到父母时,如何将其放入新的User对象中?

EDIT 06/17/15 编辑15年6月17日

For more information, here are my user model 有关更多信息,这是我的用户模型

    class User < ActiveRecord::Base
      belongs_to :parent, class_name: "User", foreign_key: "parent_id"
      has_many :children, class_name: "User", foreign_key: 'parent_id'
    end

in this situation one child can have only one parent, and one parent can have as much children as he want. 在这种情况下,一个孩子只能有一个父母,而一个父母可以拥有任意数量的孩子。
(any correction on model is welcome!) (欢迎对模型进行任何校正!)

now I can't write referrer.referrees.create(sign_up_params) but I need to do something like this (really simplified instead of my real code) 现在我无法编写referrer.referrees.create(sign_up_params)但是我需要做类似的事情(真正简化而不是我的真实代码)

    def create
      super
      parent_id = get_parent_id_from_token(params[:token])
      parent = User.find(parent_id)
      @user.parent = parent
    end

whit this code I get this error: can't write unknown attribute parent_id . 在此代码中,我收到此错误: can't write unknown attribute parent_id Where does it come from? 它从何而来?

Here's one potential solution. 这是一个潜在的解决方案。 You didn't specify what kind of relationship the two users have, but let's just assume that it's some kind of referral system where an existing user invites a friend with a token. 您没有指定两个用户之间的关系类型,而是假设这是一种推荐系统,其中现有用户邀请具有令牌的朋友。

In the model, you could set up the relationship between users: 在模型中,您可以设置用户之间的关系:

class User < ActiveRecord::Base
  belongs_to :referrer, class_name: 'User'
  has_many   :referrees, through: :referrals, class_name: 'User'
end

And then in the controller, you can leverage this relationship to handle setting up the join table entry for you: 然后在控制器中,您可以利用此关系来为您设置连接表条目:

class RegistrationsController < Devise::RegistrationsController

  def create
    referrer = User.find_by(token: sign_up_params[:referrer_token])
    referrer.referrees.create(sign_up_params)
    #...
  end

  private

  def sign_up_params
    params.require(:user).permit(:name, :surname, :username, :birthdate, :email, :password, :password_confirmation, :referrer_token)
  end
end

Just a rough idea, haven't tested the code or anything :) 只是一个大概的想法,还没有测试代码或其他任何东西:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM