添加到Devise的注册控制器

时间:2014-11-23 00:37:26

标签: ruby-on-rails devise

所以我通过设计和user模型建立了collective模型。我正在尝试创建一个邀请系统,以便用户可以通过电子邮件邀请某人访问该网站。当此人加入网站时,会创建user,并使用令牌自动将此新用户添加到集合中(邀请用户选择添加的集合)。我让邀请系统工作到非用户收到带有注册链接的电子邮件(在网址中有令牌)并进入注册页面。但是,我无法在提交注册表单后将此人添加到collective

由于Validation failed: Owner can't be blank操作中的行resource.collectives.push(org),我目前收到错误create。非常感谢帮助!

我的自定义注册控制器(我复制并略微修改了设计注册控制器代码)

class RegistrationsController < Devise::RegistrationsController

def new
    @token = params[:invite_token]
    build_resource({})
    @validatable = devise_mapping.validatable?
    if @validatable
      @minimum_password_length = resource_class.password_length.min
    end
    yield resource if block_given?
    respond_with self.resource

end

def create

build_resource(sign_up_params)
    #added @token
    @token = params[:invite_token]

    resource_saved = resource.save

    #added if @token logic
    if @token != nil
       org =  Invite.find_by_token(@token).collective
       resource.collectives.push(org)
    end

    yield resource if block_given?
    if resource_saved
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        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}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      @validatable = devise_mapping.validatable?
      if @validatable
        @minimum_password_length = resource_class.password_length.min
      end
      respond_with resource
    end

end

0 个答案:

没有答案