使用自定义创建覆盖“Devise :: RegistrationsController”会给出NoMethodError [英] Overwriting 'Devise::RegistrationsController' with custom create gives NoMethodError

查看:104
本文介绍了使用自定义创建覆盖“Devise :: RegistrationsController”会给出NoMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做错了,请让我知道。我想在create方法中添加几个自定义属性到用户,如果保存用户,请调用我的 Google Analytics(分析)方法。



我定义了一个新的控制器:

  class RegistrationsController< Devise :: RegistrationsController 


def create
build_resource(sign_up_params)

resource.public_id =工具:: generate_code
resource.referral_code =实用工具:: generate_code
如果resource.save

Analytics.identify(
user_id:resource.id.to_s,
traits:{email:resource.email})

产生资源如果block_given?
如果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资源
respond_with资源
end
end
end

此创建方法中唯一不同的是我添加了 referral_code public_id 之前保存并运行分析。

  Analytics.identify(
user_id:resource.id.to_s,
traits:{email:resource.email})

当我创建一个用户我是获取

  undefined方法`is_flashing_format?'for#< RegistrationsController:0x007fdba130d9a8> 

我不明白为什么这种方法不被继承。这是否是修改设计或添加属性/添加分析的正确方法?

解决方案

我想到了,我使用Devise版本3.1.1(锁定为依赖关系),上个月在3.2.0中添加了 is_flashing_format?



我已更改我的控制器中的方法是 is_navigational_format?,一切都很好!


Please let me know if I'm going about this the wrong way. I'm trying to add a couple custom attributes to a User in the create method as well as call my Analytics method if the user is saved.

I defined a new controller:

class RegistrationsController < Devise::RegistrationsController


  def create
    build_resource(sign_up_params)

    resource.public_id = Utilities::generate_code
    resource.referral_code = Utilities::generate_code
    if resource.save

      Analytics.identify(
          user_id: resource.id.to_s,
          traits: { email: resource.email })

      yield resource if block_given?
      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
      respond_with resource
    end
  end
end

The only thing different in this create method is that I'm adding referral_code and public_id before the save and running Analytics after.

      Analytics.identify(
      user_id: resource.id.to_s,
      traits: { email: resource.email })

When I create a user I'm getting

undefined method `is_flashing_format?' for #<RegistrationsController:0x007fdba130d9a8>

I don't understand why this method isn't being inherited. Is this even the proper way to modify devise or to add attributes/add analytics?

解决方案

I figured it out, I'm using Devise version 3.1.1 (locked for dependencies), it looks like is_flashing_format? was added last month in 3.2.0.

I changed the method in my controller to is_navigational_format? and all is well!

这篇关于使用自定义创建覆盖“Devise :: RegistrationsController”会给出NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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