开发者

Seeding a restful_authentication user in seeds.rb

I'm pretty sure I understand how seeding work with respect to seeds.rb, but I can't seem to use it to stick a restful_authentication User object into the database.

User.create(:login => 'admin',
            :role => Role.find_by_name('super_admin'),
            :email => 'admin@example.com',
            :password => '123123')

Am I missing something?

Edit:开发者_开发技巧 I also tried adding the password confirmation. Still nothing.


Try User.create!() using the same parameters). This will show you any validation errors in the console.


Password confirmation?


Have you turned the notification on? If so, the User model is trying to send the email notification. If you haven't configured your email server, the create operation will fail.

I had to do the following to work around the problem.

1 Modify the User model

Add a virtual attribute called dont_notify.

class User < ActiveRecord::Base
   # add a attribute
   attr_accessor dont_notify
end

2 Change the Observer code to detect the attribute.

class UserObserver < ActiveRecord::Observer

  def after_create(user)
    return if user.dont_notify? #notice this line 
    UserMailer.deliver_signup_notification(user)
  end

  def after_save(user)
    return if user.dont_notify? #notice this line
    UserMailer.deliver_activation(user) if user.recently_activated?
  end
end

3 Set the dont_notify flag during seeding

In you seed.rb set the flag.

User.create(:login => 'admin',
            :role => Role.find_by_name('super_admin'),
            :email => 'admin@example.com',
            :password => '123123',
            :password_confirmation => '123123',
            :dont_notify => true
         )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜