为#<devise :: registrationscontroller设计未定义的方法`users_url':0x00000003b299b0> </devise :: registrationscontroller:0x00000003b299b0>

时间:2013-06-06 20:14:53

标签: ruby-on-rails ruby

我通过设计注册得到了这个错误:

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

2 个答案:

答案 0 :(得分: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。

user.rb:

validates_uniqueness_of :name

答案 1 :(得分:0)

您使用Profile.create(user_id: self.id, user_name: self.name)创建的个人资料不会与任何用户相关联。要构建has_one依赖关系,请在build_resource回调中使用self上的after_create方法:

# 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