I have successfully installed and configured devise with an admin model with "option 1" of this instruction.
After installing rails_admin and visiting localhost:3000/admin I meet a login page wich is asking for an email and password. Since I have not set an admin email/password I am essentially locked out of rails_admin.
How can I register an admin email/password with rails_admin?
run on command prompt: rake db:seed then you can access with email admin#admin.com and password: "administrator".
I followed #Ganeshkunwar advice and ran rake db:seed but added the following to my seeds.rb
admin_model = RailsAdmin::AbstractModel.new(Admin)
admin_model.new(:email => '****#****.com', :password => '****', :password_confirmation => '****').save
Thanks SO!
Related
I'm trying to use capybara on a ruby on rails application to do some content testing, as well I'm using the devise gem to implement user authentication. I'm having trouble logging into my application to perform my test cases.
Initially, my scenario was as follows:
scenario "User arrives at main page" do
visit "purchase_orders#index"
page.should have_content("All")
# some more tests and checks
end
Where purchase_orders#index is the authenticated root, where a user's purchase orders are shown.
But when I was running the tests, I was getting the following error :
expected to find text "All" in "Log in to manage your orders * Email * Password Forgot your password? Remember me Sign up • Didn't receive confirmation instructions? About Us • Contact Us •
which tells me that its not getting past the log in page. I next tried adding the following to my scenario, before running the tests, to make it log in:
visit "purchase_orders#index"
fill_in('Email', :with => 'username#gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
where username and password are actual created accounts, but again it fails and doesn't get past the sign in page. Finally, I tried adding a before(:each) method, as follows, to attempt to sign users in for test cases:
before(:each) do
visit "users/sessions#new"
fill_in('Email', :with => 'nico.dubus17#gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
end
which, again, did not work for me. So my question is: What is the best practice and syntax for getting past the sign in page, and into the actual application? I've looked for documentation and answers on this, but haven't found anything.
Thank you!
Found an answer. I installed the warden gem to (gem 'warden') and factory girl gem (gem "factory_girl_rails", "~> 4.0") and ran a bundle install. I then created a user fixture with factory girl as follows in a factory.rb file in the spec folder:
# This is a user factory, to simulate a user object
FactoryGirl.define do
factory :user, class: User do
first_name "John"
last_name "Doe"
email "nico_dubus#hotmail.com"
encrypted_password "password"
end
end
in my rails helper file, I added this line to be able to use FactoryGirl's methods without calling it on the class every time:
config.include FactoryGirl::Syntax::Methods
Afterwards, I added these lines to the top of my capybara test page:
include Warden::Test::Helpers
Warden.test_mode!
Finally, I built a user object to use within the scope of the test:
user = FactoryGirl.build(:user)
And whenever I need to log in, I use warden's log in method
login_as(user, :scope => :user)
And voila!
I am following railscast #275 with testing the forgot me password. I am having troubles getting past the email has already been taken error. With the coding I have by following the tutorial I am suppose to receive this error, "error for no link with title or text "password". Instead this is what I am getting, "Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)"
I have done a search, unable to find a solution for it.
Here's password_resets_spec.rb:
require 'spec_helper'
describe "PasswordResets" do
it "emails user when requesting password reset"
user = FactoryGirl.create(:user)
visit login_path
click_link "password"
fill_in "Email", :with => user.email
click_button "Reset Password"
end
factories.rb:
FactoryGirl.define do
factory :user do
sequence :email do |n| "test#{n}#example.com"
end
password "secret"
end
end
Here's what I did when I finally notice it started to work. I installed database cleaner. Then I did the commands:
rake db:reset
rake db:migrate
rake db:test:prepare
Following that I noticed I had to add a "do" to the end of " it "emails user when requesting password reset".
Now I have no errors and I can continue in my testing adventure. Thanks to those who tried to assist.
The factory definition seems OK.
I would start by making sure that your test database is empty before running the spec. There may be an existing user record "test1#example.com" lingering. Also, have you tried to run only that example? Does it make any difference?
Looks like while running your test cases the factory that was created didn't rolled back/deleted the record.
add a before(:each) deletes previous records before the example executes
before(:each) do
User.delete_all
end
I had this working in an old project, but it's possible this changed in one of the more recent versions. I'm currently using Devise 2.0.4. I'm attempting to create a new user during my migration using
User.create :email => '[password]',
:password => '[password]',
:password_confirmation => '[password]'
but when I do this, it aborts with the following error
rake aborted!
An error has occurred, this and all later migrations canceled:
ActionView::Template::Error
Tasks: TOP => db:migrate:reset => db:migrate
(See full trace by running task with --trace)
Any help on this would be greatly appreciated!
Alternatively, I could create the user using the rails shell, but for consistency, I'd like to have one default user to get started every time.
Based on prasvin's comment, I found that the better way to do this was to go into db/seeds.rb and populate a seed element such as
User.create(:email => '[email]', :password => '[password]', :password_confirmation => '[password]')
This itself lead to a different error message
rake aborted!
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Which I eventually found through a different post was related to the fact that in my config/environments/development.rb file I had not yet set
config.action_mailer.default_url_options = { :host => 'localhost:3000'}
Which means that when Devise was trying to send out the confirmation email, it failed because it didn't know what address to tell them to come back to. This is what caused the ActionView::Template::Error. Once that is all done, it works perfectly as expected.
While running an app how do you select a user by email address and then set the password manually within rails console for Devise?
Also, where would I go to review documentation to cover more details in this regard to manipulation of accounts while using Devise?
Modern devise allows simpler syntax, no need to set the confirmation field
user.password = new_password; user.save
# or
user.update(password: new_password)
# $ rails console production
u=User.where(:email => 'usermail#gmail.com').first
u.password='userpassword'
u.password_confirmation='userpassword'
u.save!
If you run the following in the rails console it should do the trick:
User.find_by(email: 'user_email_address').reset_password!('new_password','new_password')
http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable
You can simply update password field, no need for confirmation password, devise will save it in encrypted form
u = User.find_by_email('user#example.com')
u.update_attribute(:password, '123123')
For some reason, (Rails 2.3??)
user = User.where(:email => email).first
didn't work for me, but
user = User.find_by_email('user#example.com')
did it.
1.Login in to ralis console
$ sudo bundle exec rails console production
2.Then update the administrator's password
irb(main):001:0> user = User.where("username = 'root'")
irb(main):002:0> u = user.first
irb(main):003:0> u.password="root2014#Robin"
=> "root2014#Robin"
irb(main):004:0> u.password_confirmation="root2014#Robin"
=> "root2014#Robin"
irb(main):005:0> u.save
=> true
irb(main):006:0> exit
3.Refresh the login page, use the new password to login, enjoy!
Good Luck!
User.find_by_email('joe#example.com').update_attributes(:password => 'password')
If your account is locked from too many login attempts, you may also need to do:
user.locked_at = ''
user.failed_attempts = '0'
user.save!
I've been using the Authlogic rails plugin. Really all I am using it for is to have one admin user who can edit the site. It's not a site where people sign up accounts. I'm going to end up making the create user method restricted by an already logged in user, but of course, when I clear the DB I can't create a user, so I have to prepopulate it somehow. I tried just making a migration to put a dump of a user I created but that doesn't work and seems pretty hacky. What's the best way to handle this? It's tricky since the passwords get hashed, so I feel like I have to create one and then pull out the hashed entries...
Rails 2.3.4 adds a new feature to seed databases.
You can add in your seed in db/seed.rb file:
User.create(:username => "admin", :password => "notthis", :password_confirmation => "notthis", :email => "admin#example.com")
Then insert it with:
rake db:seed
for production or test
RAILS_ENV="production" rake db:seed
RAILS_ENV="test" rake db:seed
My favorite feature in 2.3.4 so far
If you are using >= Rails 2.3.4 the new features include a db/seeds.rb file. This is now the default file for seeding data.
In there you can simple use your models like User.create(:login=>"admin", :etc => :etc) to create your data.
With this approach rake db:setup will also seed the data as will rake db:seed if you already have the DB.
In older projects I've sometimes used a fixture (remeber to change the password straight away) with something like users.yml:
admin:
id: 1
email: admin#domain.com
login: admin
crypted_password: a4a4e4809f0a285e76bb6b35f97c9323e912adca
salt: 7e8455432de1ab5f3fE0e724b1e71500a29ab5ca
created_at: <%= Time.now.to_s :db %>
updated_at: <%= Time.now.to_s :db %>
rake db:fixtures:load FIXTURES=users
Or finally as the other guys have said you have the rake task option, hope that helps.
Most used approach is to have a rake task that is run after deployment to host with empty database.
Add a rake task:
# Add whatever fields you validate in user model
# for me only username and password
desc 'Add Admin: rake add_admin username=some_admin password=some_pass'
task :add_admin => :environment do
User.create!(:username=> ENV["username"], :password=> ENV["password"],:password_confirmation => ENV["password"])
end