Barna Kovacs
Barna Kovacs

Reputation: 1276

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

I get this error with devise registration:

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

With Omniauth facebook, login, everything works fine.

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

What could be the solution to get this working with devise registration?

Important: It works with omniauth facebook, but it doesn't work with devise registration.

Edit: i get this error with Profile.create! method:

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 repo: https://github.com/gwuix2/anabol

Github issue:

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

Upvotes: 0

Views: 2888

Answers (2)

Barna Kovacs
Barna Kovacs

Reputation: 1276

Problem solved:

in profile.rb:

validates_uniqueness_of :slug

caused the problem, because for Profile.user_name the following in user.rb:

after_save :myprofile

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

Problem solved by adding an uniqueness validation to User.name, which then passes as unique to the Profile, which then creates a unique :slug.

user.rb:

validates_uniqueness_of :name

Upvotes: 1

zeantsoi
zeantsoi

Reputation: 26193

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

Upvotes: 0

Related Questions