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
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 have two models: Account and Profile. In Account I have role column. I need to add role column to profiles table.
I include in Profile has_one :account association and belongs_to :profile in Account model. I think the solution is near that.
profile.rb
class Profile < ApplicationRecord
has_one :account
end
acount.rb
class Account < ApplicationRecord
devise :registerable, :database_authenticatable, :rememberable,
:trackable, :confirmable, :lockable, :recoverable, :validatable
belongs_to :profile, foreign_key: 'profile_id'
enum role: %i[user admin]
end
schema.rb
create_table "accounts", 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.string "remember_token"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "profile_id", null: false
t.integer "role", default: 0
t.index ["profile_id"], name: "index_accounts_on_profile_id"
end
create_table "profiles", force: :cascade do |t|
t.string "last_name", null: false
t.string "first_name", null: false
t.string "middle_name"
t.string "email", null: false
t.text "about"
t.date "hire_date", null: false
end
I want to which is in profiles table to be added in column Account.role.
how about just using a function which access the role data from accounts
class Profile < ApplicationRecord
def role
return account.role unless account.nil?
end
end
This is my first publication in stack overflow, I hope you can help me.
I'm working with RoR and PostgreSQL, gem 'devise'.
In rails console I am trying to delete data from the "Competitor" table, but I have the following error and I have not been able to solve it.
2.4.1 :006 > c.destroy(c)
ActiveRecord::UnknownPrimaryKey: Unknown primary key for table competitors in model Competitor.
This is my competitors table, which it's was generate with model of gem devise
create_table "competitors", id: false, 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.bigint "rut"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "lastname"
t.integer "phone"
t.date "dateOfBirth"
t.boolean "gender"
t.string "numberSerie"
t.string "otp_secret_key"
t.integer "otp_module", default: 0
t.index ["email"], name: "index_competitors_on_email", unique: true
t.index ["reset_password_token"], name: "index_competitors_on_reset_password_token", unique: true
t.index ["rut"], name: "index_competitors_on_rut", unique: true
endere
and this is the model
class Competitor < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :raffle_registers, primary_key: 'rut', foreign_key: 'rutCompetitors'
has_many :accountpays
has_one :found
#otp model to make it use TFA
has_one_time_password
enum otp_module: { disabled: 0, enabled: 1 }, _prefix: true
attr_accessor :otp_code_token
end
You generated the competitors table without primary key. Checkout this line:
create_table "competitors", id: false, force: :cascade do |t|
The id: false is your issue. Check the migration to create the competitors table and set a primary key (or create a new migration adding a primary key to it).
Useful resource: https://stackoverflow.com/a/10079409/740394
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 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