I can`t create admin user on devise - ruby-on-rails-3

I'm using Rails 3.2.13 and Devise.
Devise is working fine but I cant create“admin”` user.
I followed this https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role#option-2---adding-an-admin-attribute
and added the admin attribute to the users table.
I tried to insert the admin using this topic: Creating an admin user in Devise on Rails beta 3
This is User model:
User(id: integer, name: string, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, admin: boolean)
And this is my seed file:
user = User.create!(name: "admin", email: "admin#admin.com", password:
"admin", password_confirmation: "admin", encrypted_password: "admin",
admin: true)
But when I execute rake db:seed, active record shows me an error message:
invalid_record
I don`t know how I can fix it.

I just changed the information in seeds.rb to:
user = User.create!(name: 'admin', email: 'admin#admin.com', password:
'admin123456', password_confirmation: 'admin123456', admin: true)
I wasn't able to insert the User admin because password was too short.

Related

Invite user without email + rails + devise invitable

I want to have a feature where I can invite any user using only their phone_number. I don't have the email address. When a user is being invited, an SMS is sent to the specified number and depending upon the contents of the reply(say, 'Y' or 'N') SMS from user, their account is confirmed/rejected. We plan to provide a link in the sms where in the user can click and enter the required details(email, name etc).
Loading development environment (Rails 5.0.2)
2.3.0 :001 > u = User.invite!(phone_number: '1234567890')
=> #<User id: nil, email: nil, first_name: nil, last_name: nil, phone_number: "1234567890", created_at: nil, updated_at: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>
2.3.0 :002 > u.errors
=> #<ActiveModel::Errors:0x007fc91fcd4c00 #base=#<User id: nil, email: nil, first_name: nil, last_name: nil, phone_number: "1234567890", created_at: nil, updated_at: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>, #messages={:email=>["can't be blank"]}, #details={:email=>[{:error=>:blank}]}>
I have same problem and i set example email for it and remove it later...
u = User.invite!(phone_number: '1234567890', email: 'example#abc.com', skip_invitation: true)
u.update(email: '')
Add this to user.rb:
def email_required?
false
end
def email_changed?
false
end

Ruby on Rails 4: has_secure_password is not asking for a password confirmation when creating new user

I am currently writing an app using RoR4 and am having trouble with authentication. Even though I've added has_secure_password to the User model, I still seem to be able to create a new user without having to provide a password confirmation.
2.1.2 :012 > me = User.create(:institution_id => 1, :email => "me#myschool.edu", :password => "mypassword")
(0.1ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'me#myschool.edu' LIMIT 1
SQL (0.2ms) INSERT INTO `users` (`created_at`, `email`, `institution_id`, `password_digest`, `updated_at`) VALUES ('2014-07-14 20:02:34', 'me#myschool.edu', 1, '$2a$10$sD2N.2nxhYO7egzzWxfF5.cdIZ4ds41.sU93Ja3E9Q0qAOaABdZb6', '2014-07-14 20:02:34')
(8.2ms) COMMIT
=> #<User id: 5, institution_id: 1, first_name: nil, last_name: nil, email: "me#myschool.edu", blurb: nil, facebook_link: nil, facebook_token: nil, password_digest: "$2a$10$sD2N.2nxhYO7egzzWxfF5.cdIZ4ds41.sU93Ja3E9Q0...", api_key: nil, active: false, created_at: "2014-07-14 20:02:34", updated_at: "2014-07-14 20:02:34", authentication_token: nil>
Why is this happening? Shouldn't has_secure_password always require a password confirmation as well??
Thanks for the help
Make sure you followed these things:
add gem 'bcrypt' to gem file.
Next:
have a password_digest attribute
Next:
class User < ActiveRecord::Base
has_secure_password
end
Then you can test it:
user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
user.save # => false, password required
user.password = 'mUc3m00RsqyRe'
user.save # => false, confirmation doesn't match
user.password_confirmation = 'mUc3m00RsqyRe'
user.save # => true
user.authenticate('notright') # => false
user.authenticate('mUc3m00RsqyRe') # => user
User.find_by(name: 'david').try(:authenticate, 'notright') # => false
User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user

Rails User.all bug found in Hartl's scaffold app - Why is User.all not responding to command like an array of hashes?

I discovered this bug while reproducing Michael Hartl's scaffolded rails demo_app from his rails tutorial.
I went into the rails console
rails c
I asked for the User.all array of hashes and out it through its paces:
2.0.0-p0 :012 > User.all
User Load (0.3ms) SELECT "users".* FROM "users"
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
2.0.0-p0 :013 > User.all[0]
User Load (0.2ms) SELECT "users".* FROM "users"
=> #<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">
2.0.0-p0 :015 > User.all[0]['id']
User Load (0.2ms) SELECT "users".* FROM "users"
=> 2
So far, so good.
However, User.all does not respond to the command to list say just the id's or names of all users:
2.0.0-p0 :017 > User.all { |i| puts i['id'] }
User Load (0.2ms) SELECT "users".* FROM "users"
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06"gt;]
2.0.0-p0 :019 >User.all{ |i| puts i['name'] }
User Load (0.3ms) SELECT "users".* FROM "users"
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
Assigning an arbitrary variable a to the array of hashes User.all resolves the issue:
2.0.0-p0 :021 >a.each {|i| puts i['id'] }
2
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
2.0.0-p0 :022 >a.each {|i| puts i['name'] }
Lisa Johnson
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
This User.all issue affects at least ruby versions 1.9.2,1.9.3 and 2.0.0. Whoever is responsible for writing the gem that created User.all needs to go over his all method. For whatever it's worth, I am working with rails 3.2.12
Nothing to discuss - its a straightforward bug report. If there is a question, the question would be "why is User.all is not behaving like the array of hashes it is?"

Issue using RSpec to pass parameters to mocked model method in Rails controller action

I'm trying to mock a user models save method to return false, and simulate what the controller would do in case of validation failure.
My spec looks like:
require 'spec_helper'
describe UsersController do
describe 'POST create_email_user' do
it 'responds with errors json if saving the model fails' do
#params = {
:last_name => 'jones',
:email => 'asdfasdfasfd#'
}
User.stub(:new_email_user) .with(#params) .and_return(#mock_user)
#mock_user.stub(:save) .and_return(false)
post :create_email_user, :user => #params, :format => :json
response.body.should == #mock_user.errors.as_json
response.status.should == :unprocessable_entity
end
end
end
Controller action looks like:
class UsersController < ApplicationController
def create_email_user
#user = User.new_email_user(params[:user])
if #user.save
# code left out for demo purposes
else
render json: #user.errors, status: :unprocessable_entity
end
end
end
When running this spec, I get the error that basically says the expected call to new_email_user got the wrong arguments:
1) UsersController POST create_email_user responds with errors json if saving the model fails
Failure/Error: post :create_email_user, :user => #params, :format => :json
<User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, uid: string, provider: string, first_name: string, last_name: string, registration_id: integer, gender: string, authentication_token: string) (class)> received :new_email_user with unexpected arguments
expected: ({:last_name=>"jones", :email=>"asdfasdfasfd#"})
got: (["last_name", "jones"], ["email", "asdfasdfasfd#"])
Please stub a default value first if message might be received with other args as well.
It seems like rails converts the has to an array of key value pairs, while my code is just using the straight hash in the setup.
How can I simulate this converting to an array, or am I doing else wrong?
Drop the .with stub on user, since it isn't really relevant to the test and it is causing problems.
Without the .with the stub will return that value for any arguments.
So:
User.stub(:new_email_user).and_return(#mock_user)
I would then have a separate spec which ensures that:
User.should_receive(:new_email_user).with(#params)
And diagnose that problem in its own, narrow spec.

setting an atttribute in rails console - transaction rolled back

I've used this forum gem before and also been able to set the forem_admin to "true" using the console. However, this time it's rolling back the transaction when I try to save.
You can see below that I set the forem_admin to "true," saved it, it rolled back the transaction (which means it didn't save), I did "u" and it showed forem_admin = true but when I restarted the console it showed forem_admin = false.
If anyone's familiar with this gem, I think this forem_state attribute is new, so I'm wondering if anything's changed.
I'm not an SQL pro but one thing that looks funny to me is the line
"users"."id" != 5
I would have expected it to have been "users"."id" = 5 i.e. with no "!", because the id of the user "signuplinks" is 5.
this is a rails 3.2 app
update when I do u.errors after u.save, I'm getting this error message
#messages={:password=>["can't be blank"]}
Console
ruby-1.9.3-rc1 :001 > u = User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: false, forem_state: "pending_review">
ruby-1.9.3-rc1 :002 > u.forem_admin = true
=> true
ruby-1.9.3-rc1 :003 > u.save
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE (LOWER("users"."name") = LOWER('signuplinks') AND "users"."id" != 5) LIMIT 1
User Exists (0.1ms) SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('signuplinks#gmail.com') AND "users"."id" != 5) LIMIT 1
(0.1ms) rollback transaction
=> false
ruby-1.9.3-rc1 :004 > u
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: true, forem_state: "pending_review">
ruby-1.9.3-rc1 :005 > exit
Michael-Pro:ic michl$ rails c
Loading development environment (Rails 3.2.1)
ruby-1.9.3-rc1 :001 > u = User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: false, forem_state: "pending_review">
actually, this was my own fault. Thanks to #muistooshort for pointing out problem probably is in model. I had included "password" in the validates_presence of validator which was stopping me from changing user details in the console (without including password)