0

我在设计注册时收到此错误:

undefined method `users_url' for #<Devise::RegistrationsController:0x00000003b299b0>

使用 Omniauth facebook,登录,一切正常。

class User < ActiveRecord::Base
  has_one :profile, :dependent => :destroy

  after_save :myprofile

  def myprofile
    if self.profile
    else
      Profile.create(user_id: self.id, user_name: self.name)
    end
  end
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

使用设计注册进行此工作的解决方案可能是什么?

重要提示:它适用于 omniauth facebook,但不适用于设计注册。

编辑:我在 Profile.create 中收到此错误!方法:

NoMethodError - undefined method `users_url' for #<Devise::RegistrationsController:0x00000005946e20>:
  actionpack (3.2.13) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
  actionpack (3.2.13) lib/action_dispatch/routing/url_for.rb:150:in `url_for'
  actionpack (3.2.13) lib/action_controller/metal/redirecting.rb:105:in `_compute_redirect_to_location'
  actionpack (3.2.13) lib/action_controller/metal/redirecting.rb:74:in `redirect_to'
  actionpack (3.2.13) lib/action_controller/metal/flash.rb:25:in `redirect_to'

Edit_2: Github存储库: https ://github.com/gwuix2/anabol

Github问题:

https://github.com/plataformatec/devise/issues/2457

4

2 回答 2

1

问题解决了:

在 profile.rb 中:

validates_uniqueness_of :slug

导致问题,因为对于 Profile.user_name 在 user.rb 中有以下内容:

after_save :myprofile

def myprofile
  Profile.create!(user_id: self.id, user_name: self.name)
end

通过向 User.name 添加唯一性验证来解决问题,然后将其作为唯一性传递给配置文件,然后创建一个唯一的 :slug。

用户.rb:

validates_uniqueness_of :name
于 2013-06-08T08:53:45.440 回答
0

The profile you're creating with Profile.create(user_id: self.id, user_name: self.name) isn't associated with any User. In order to build a has_one dependency, use the build_resource method on self in an after_create callback:

# app/models/user.rb
class User < ActiveRecord::Base
    has_one :profile, :dependent => :destroy

    after_create :myprofile

    def myprofile
        profile = Profile.create!(user_id: self.id, user_name: self.name) # `.create!` will throw an error if this fails, so it's good for debugging
        self.profile = profile
    end
end
于 2013-06-06T20:35:43.447 回答