Devise错误:undefined方法`current_sign_in_ip' [英] Devise error: undefined method `current_sign_in_ip'

查看:136
本文介绍了Devise错误:undefined方法`current_sign_in_ip'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将Devise安装到我的Rails 4.0.10应用程序中。我希望用户可以跟踪,但是我决定不需要:current_sign_in_ip :last_sign_in_ip ,所以我删除这些属性。



我的迁移:

  ##可追踪
t.integer:sign_in_count,默认值:0,null:false
t.datetime:current_sign_in_at
t.datetime:last_sign_in_at
t.string:initial_ip
#t。 inet:current_sign_in_ip
#t.inet:last_sign_in_ip

当我试图让我的第一个用户,我收到这个错误:

  Devise :: RegistrationsController中的NoMethodError#create 
undefined方法`current_sign_in_ip'为#< ;用户:XXXXXXXXX>

我不明白为什么 current_sign_in_ip 甚至被称为。这是创建操作,它绝对不包括这个方法:

  def create 
build_resource(sign_up_params)

resource.save
产生资源如果block_given?
如果resource.persisted?
如果resource.active_for_authentication?
set_flash_message:notice,:signed_up if is_flashing_format?
sign_up(resource_name,resource)
respond_with资源,位置:after_sign_up_path_for(资源)
else
set_flash_message:通知,:signed_up_but _#{resource.inactive_message}如果is_flashing_format?
expire_data_after_sign_in!
respond_with资源,位置:after_inactive_sign_up_path_for(资源)
end
else
clean_up_passwords资源
set_minimum_password_length
respond_with资源
end
end

为什么 current_sign_in_ip 甚至被调用?

解决方案

错误是因为您没有获得:current_sign_in_ip :last_sign_in_ip 属性在您的数据表。



这就是为什么:

 #t.inet:current_sign_in_ip 
#t.inet:last_sign_in_ip

在原始迁移中注释出来,阻止他们被附加。你知道这一点。



mentanco 的答案所提到的,要修复的方法是将它们放回进入表格,或删除



以下是迁移以添加额外数据的方法:

  $ rails g迁移AddTrackingToUsers 

#db / migrate / add_tracking_to_users _________。rb
class AddTrackingToUsers
def change
add_column:users,:current_sign_in_ip,:string
add_column:users ,:last_sign_in_ip,:string
end
end

$ rake db:migrate

如果运行这个,它将添加属性到表格,并重新获得功能,或者,您可以从Devise模型中删除:trackable ,以使其完全跳过此过程。


I just installed Devise into my Rails 4.0.10 app. I wanted by users to be trackable, but I decided I didn't need :current_sign_in_ip and :last_sign_in_ip, so I removed those attributes.

My migration:

  ## Trackable
  t.integer  :sign_in_count, default: 0, null: false
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :initial_ip
  # t.inet     :current_sign_in_ip
  # t.inet     :last_sign_in_ip

After I tried to make my first user, I got this error:

NoMethodError in Devise::RegistrationsController#create
undefined method `current_sign_in_ip' for #<User:xxxxxxxxx>

I don't understand why current_sign_in_ip is even being called. Here's the "Create" action, which definitely doesn't include that method:

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 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
    set_minimum_password_length
    respond_with resource
  end
end

Why is current_sign_in_ip even being called at all?

解决方案

The error is caused because you've not got the :current_sign_in_ip and :last_sign_in_ip attributes in your data table.

This is the reason why:

 # t.inet     :current_sign_in_ip
 # t.inet     :last_sign_in_ip

Commenting out those in your original migration has prevented them from being appended. You know this.

The way to fix, as mentioned in the answer by mentanco is to either put them back into the table, or to remove the :trackable reference from Devise...

Here's how to get the migration to add the extra data:

$ rails g migration AddTrackingToUsers

#db/migrate/add_tracking_to_users_________.rb
class AddTrackingToUsers
   def change
      add_column :users, :current_sign_in_ip, :string
      add_column :users, :last_sign_in_ip, :string
   end
end

$ rake db:migrate

If you run this, it will add the attributes to the table and get the functionality working again, alternatively, you can remove :trackable from your Devise model to allow it to skip the process entirely.

这篇关于Devise错误:undefined方法`current_sign_in_ip'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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