I tried using Devise but since that didn't work out for me, I decided to build the authentication and sessions from scratch. I realize the problem wasn't devise, but it's mysql. For some reason, when I register a user, the information and attributes are stored in the database. When I login using the email and password, it keeps telling me that it's an invalid email/password. The log looks like this:
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NFudGruZS79uwDrKzbHDQrBjlcwQ7AkC958vI4aHDAs=", "session"=>{"email"=>"first#abc.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`email` IS NULL LIMIT 1
After some investigation, I know it's not because I didn't install mysql or other gems properly because a brand new app works just fine.
Does anyone know why it's not pulling the email I entered and pulling email is NULL instead?
Should I just create a new database and switch my database.yml file to the new database instead?
Thanks in advance.
EDIT - SOME MORE CODE
user model:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :secondary_email, :password, :password_confirmation, :gender
has_many :user_owner_relationships
has_many :owners, :through => :user_owner_relationships
has_many :cash_endowments
has_many :checks, :through => :cash_endowments
has_many :owners, :through => :cash_endowments
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
:uniqueness => { :case_sensitive => false },
:format => { :with => email_regex }
validates :name, :presence => true,
:length => { :maximum => 40 }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..20 }
sessions_controller (user controller is standard)
def new
#title = "Sign in"
end
def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to root_path, :notice => "Welcome '#{user.first_name}"
else
flash.now.alert = "Invalid email or password."
render "new"
end
end
Rookie mistake -
It's actually because my new.html.erb for the sessions controller was not pulling the :session symbol from this code:
<%= form_for (:session, :url => sessions_path) do |f| %>
So I just ended up using form_tag instead.
Related
I have _form.html.erb. and on the first line of code, it is throwing this error:
undefined method `assessments_path' for #ActionView::Base:0x00000000229750
Did you mean? asset_path
couple of points:
in routes 'poweruser' is a namespace
in models assessment.rb is
not folder poweruser (as I plan to use it for other pages that anyone can access not just powerusers).
Any ideas what I am missing?
Here is the code:
<%= form_with(model: [#assessment], local: true) do |form| %>
<%= render "shared/error_messages", resource: form.object %>
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control" %>
</div>
<% end %>
Here is the controller
module Powerusers
class AssessmentsController < ApplicationController
before_action :authenticate_user!
# before_action :set_assessment
# before_action :set_assessment, only: [:set_assessment, :show, :edit, :update, :destroy]
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# super
# send_foo_updated_email(requested_resource)
# end
def index
#pagy, #assessments = pagy(Assessment.sort_by_params(params[:sort], sort_direction))
# We explicitly load the records to avoid triggering multiple DB calls in the views when checking if records exist and iterating over them.
# Calling #assessments.any? in the view will use the loaded records to check existence instead of making an extra DB call.
#assessments.load
end
# GET /assessments/new
def new
#assessment = Assessment.new
#assessment.assessment_sections.new
end
here is the assessment.rb (not it is not in a sub-folder poweruser)
class Assessment < ApplicationRecord
belongs_to :company
has_many :assessment_sections, inverse_of: :assessment
has_many :questions, through: :assessment_sections
accepts_nested_attributes_for :assessment_sections, reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :questions, reject_if: :all_blank,
allow_destroy: true
end
Here is the routes
# PowerUser
authenticated :user, lambda { |u| u.admin? } do
namespace :powerusers do
resources :assessments do
resources :assessment_sections do
resources :questions
end
end
end
end
As you have namespace in routes, you need to specify that with form_with
<%= form_with(model: [:powerusers, #assessment], local: true) do |form| %>
I am working through the Hartl tutorial and I have run into a problem. When I attempt to add a user in the console it doesn't return that it has saved any of the information I just entered. The code is below, hopefully you guys can steer me in the right direction.
The User model:
class User < ActiveRecord::Base
attr_accessor :name, :email
before_save { self.email = email.downcase}
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 255},
format: { with: VALID_EMAIL_REGEX },
uniqueness: {case_sensitive: false}
has_secure_password
validates :password, length: { minimum: 6 }
end
the error:
2.2.0 :003 > User.create(name: "Jim Bob", email: "jim#bob.com", password: "jimmybob", password_confirmation: "jimmybob")
(0.1ms) begin transaction
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('jim#bob.com') LIMIT 1
SQL (0.4ms) INSERT INTO "users" ("password_digest", "created_at", "updated_at") VALUES (?, ?, ?) [["password_digest", "$2a$10$I61hwA3iAQmzsT/wbb0wpOSannOBNPLtQsBhscjakS5OgCi6zJoMq"], ["created_at", "2015-02-13 23:45:32.636129"], ["updated_at", "2015-02-13 23:45:32.636129"]]
(137.3ms) commit transaction
=> #<User id: 1, name: nil, email: nil, created_at: "2015-02-13 23:45:32", updated_at: "2015-02-13 23:45:32", password_digest: "$2a$10$I61hwA3iAQmzsT/wbb0wpOSannOBNPLtQsBhscjakS5...">
I don't understand why the name and the email will not save. Please any help would be amazing.
Its because you have defined attr_accessor for name and email.
attr_accessor will overwrite the methods generated by active records so its not been saved to database.
I am having legacy database
Rails 3.2 & ruby 1.9.3p448
I wanted to develop active_admin back-end application
I wanted use CarrierWave File Uploads gem for uploading image
Following is my code in admin/item_master.rb i.e in view
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Item Details" do
f.input :IMTNAME
f.input :IMTBRIEFDESC
f.input :IMTDETAILDESC
f.input :IMTIMAGE, :as => :file
end
f.actions
end
Following is my code in model
class ItemMaster < ActiveRecord::Base
set_table_name "MDIMT"
attr_accessible :IMTNAME, :IMTBRIEFDESC, :IMTDETAILDESC, :IMTIMAGE
mount_uploader :IMTIMAGE, ImtimageUploader
end
It givesme Following Error
NameError in Admin::ItemMastersController#update
uninitialized constant IMTIMAGE
Im having an issue with MySql.Created a boolean column(Via a Database Migration) in MySql Database,toggled the value to "true".I Have a method in my rails App that checks if the value in that column is true,but it always returns false(even when called from a ruby console). Tried using the Same Migration on a SQLite Database and the same code returns true on the same Column. Observed MySQL uses tinyint for boolean values(1 == true) .
Please how can i correct this?
My Migration:
def self.up
create_table :users do |t|
t.string :name
t.string :email
t.boolean :admin , default => false
...
t.timestamps
end
end
...
Then i used the Faker Gem to Populate my Database, toggling one user in it setting it's value to true
Sample Data Via Faker Gem
admin = User.create!(:name => "sample",
:email => "sample#sample.com",
:password => "sample",
:password_confirmation => "sample")
admin.toggle!(:admin) #toggling value of admin to true just for this user
I am using devise as my authentication system. And i want to
authenticate user with username along with subdomain.
It seems that devise needs both the username and subdomain field in
the same table which is not in my case.
I have subdomain field in Company table while username and password in
the UserAccount table.
And there is references_many relation between UserAccount and Company table
Now how can i authenticate the user with both username and subdomain
Please help me out.
!#user.rb
devise :all, :authentication_keys => [:email, :subdomain]
OR
!#config/initializer/devise.rb
Devise.setup do |config|
config.authentication_keys = [ :email, :subdomain ]
end
!#login.erb.html
...
f.hidden_field :subdomain, :value => current_subdomain
...
!#user.rb
def self.find_for_authentication(conditions={})
find(:first, :conditions => { :companies => { :subdomain => conditions.delete(:subdomain) } }, :joins => :companies)
end