简体   繁体   中英

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.

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. 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:

    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? on the sign_up_params function like this?

    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?

EDIT 06/17/15

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)

    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 . 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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