I am using Devise to login users to the application.
I am trying to pass following test. Firstname and lastname pass but "expect(User.new).to_not be_admin" doesn't passs.
Each time I get message as below.
"Failure/Error: expect(User.new).to_not be_admin expected # to respond to 'admin?'"
Any suggestions how to pass such test? I think the problem is in to_not be_admin - but I'm really new to testing software. I've googled for this but with no result. Many thanks in advance for help.
spec/models/user_spec.rb
require 'spec_helper'
describe User do
it { should validate_presence_of (:firstname) }
it { should validate_presence_of (:lastname) }
it "by default isn't admin" do
expect(User.new).to_not be_admin
end
end
User Model (User.rb):
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :reviews
has_many :products
validates_presence_of :firstname
validates_presence_of :lastname
end
My users table looks like this:
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "firstname"
t.string "lastname"
t.boolean "admin", default: false
end
Related
I am new to rails and currently working on a dating application as a portfolio project. However, on my home page, I want to be able to display/render Male or Female data based on the gender of the current_user. That is if the logged-in user is a male, he will only be able to view people of the opposite gender since it's a dating application.
I am using Devise gem and included a custom field called gender. Please, someone should help me with how to do this.
class UsersController < ApplicationController
def index
#user = current_user
#users = User.all
end
def show
#user = User.find(params[:id])
end
User Table
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.text "short_bio"
t.string "gender"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "like"
t.string "image_url"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
User Model
class User < ApplicationRecord
has_one_attached :image
has_many_attached :pictures
has_many :messages
has_many :likes
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :validatable, :trackable
# validates :short_bio, presence: true, length: {maximum: 500}
validates :email, presence: true, uniqueness: { case_sensitive: false }, length: {minimum: 5, maximum: 50}
end
You can only load users with the opposite gender than the current user like this:
#users = User.where.not(gender: current_user.gender)
Probably you need in the model two columns. It is just example as idea
You can use gender (it's not binary) instead of sex
You can use other data type of course
t.integer :sex
t.integer :look_for_sex
And for example:
0 -- female, 1 -- male
And then
User.where(sex: current_user.look_for_sex, look_for_sex: current_user.sex)
will return only users that current user want to date
Thank you for all that responded. I didn't have to refactor my code but just added the code below and that fixed my problem:
#users = current_user == 'Male' ? User.where(gender: 'Male') : User.where(gender: 'Female')
In this case, if the current user is a 'Male', he will only have access to 'Female' and vice versa.
I’m new to unit test and Rails in general. I have a simple Rails application and trying unit test it with the default Minitest and Capybara. Unfortunately, its giving me this error regardless of what's I’m tiring to test
E
Error:
StocksControllerTest#test_the_truth:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: admins.email: INSERT INTO "admins" ("created_at", "updated_at", "id") VALUES ('2017-06-28 08:49:33.148641', '2017-06-28 08:49:33.148641', 298486374)
bin/rails test test/controllers/stocks_controller_test.rb:10
test/controllers/stocks_controller.rb
require 'test_helper'
class StocksControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Stationery Management System"
end
test "the truth" do
assert true
end
end
Schema.rb
ActiveRecord::Schema.define(version: 20170627120530) do
create_table "admins", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_admins_on_email", unique: true
t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true
end
create_table "stocks", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "admin_id"
t.index ["admin_id"], name: "index_stocks_on_admin_id"
end
end
models/stock.rb
class Stock < ApplicationRecord
belongs_to :admin
end
models/admin.rb
class Admin < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :stocks, dependent: :destroy
end
Keep in mind that the application seem to function pretty well. I did try to drop reset my test DB but it didn’t help. Would be graceful for any help.
I'm following a guide on Devise and I am currently stuck with this:
`NoMethodError in TasksController#new undefined method `tasks' for nil:NilClass
Task.rb
class Task < ActiveRecord::Base
belongs_to :user
end
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :tasks
end
TaskController.rb
def new
#task = current_user.tasks.build
end
def create
#task = current_user.tasks.build(task_params)
end
DB - Schema.rb
create_table "tasks", force: :cascade do |t|
t.string "name"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Help is much appreciated!
Error message says: nil.tasks
It means the User class object is not created.
Try to login. Once you are logged in, the error will be resolved.
I have set up the necessary models and views to have a Devise resource, Account and User. Account has_many users & User belongs_to account. The schema reflects account_id as an attribute of user. I'm probably mistaken but I was under the impression that this account_id attribute would automatically be filled when an Account is logged in & creates a new user. Upon checking the Rails console, it seems all new users created this way had a nil value for account_id. Separate question but is this the ideal way to have multitenancy in an application using Devise?
Models:
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :account, :inverse_of => :users
accepts_nested_attributes_for :account
end
account.rb
class Account < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :users, :inverse_of => :account, :dependent => :destroy
accepts_nested_attributes_for :users
has_many :projects
end
schema.rb (just users & accounts)
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "account_id"
t.boolean "supervisor"
t.string "name"
end
create_table "accounts", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
end
users_controller.rb
class UsersController < ApplicationController
before_filter :authenticate_user!
def new
#user = User.new
end
def create
#user.skip_confirmation! # confirm immediately--don't require email confirmation
if #user.save
flash[:success] = "User added and activated."
redirect_to users_path # list of all users
else
render 'new'
end
end
def index
#users = User.all
end
end
accounts_controller.rb
class AccountsController < ApplicationController
def new
#accounts = Account.new
#accounts.users.build
end
def create
#account = Account.new(params[:account])
if #account.save
flash[:success] = "Account created"
redirect_to accounts_path
else
render 'new'
end
end
end
from registrations > edit.html.erb
<% if account_signed_in? %>
<div class="jumbotron">
<span><%= link_to "Add user", new_user_registration_path, class: "btn btn-primary btn-sm" %></span>
</div>
<% end %>
I am not sure what how exactly you are trying to create a user
But creating a user with
#account.users.build
would automatically add account_id to user object.
Hope this helps! :)
I am using Ruby on Rails 4 with devise for user authentication.
I have problem with creating user from console nor from edited devise registration view. I can set password and email, but i can't set username, I have to create user witouth name and then change it from dbconsole using SQL command ("UPDATE users SET..."). Then I can login with username.
My users table:
create_table "users", force: true do |t|
t.string "name", default: "", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
My users model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:rememberable, :trackable, :validatable
validates_presence_of :name, :email, :password
attr_accessor :name
end
When I try
u = User.new
u.name = "test"
from rails console name stays blank.
Try to remove this line:
attr_accessor :name