I for the first time use Devise. When generated the user model and I tried to register the first user according to the link - http://localhost:3000/users/sign_up. There is a mistake: http://i.stack.imgur.com/44uMT.png
routes.rb:
Diary::Application.routes.draw do
devise_for :users
root 'static_pages#welcome', as: 'welcome'
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
end
Thanks!
Related
I am trying to create a history from other actions inside my User model through BookingHistory model.
Like this:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :rooms
has_many :booking_histories, foreign_key: :booking_history_id
accepts_nested_attributes_for :booking_histories
def set_history(room)
history = self.booking_histories.build
history.booking = room.booking
history.room = room
history.total_price = room.booking.order
history.time_spent = room.booking.time_at_room
history.charged = false
end
end
But I'm getting this error and I don't know why. I've already made the one-to-many relationship.
THe error
Does anyone have any idea of what's going on?
I'm trying to create a record for the associated model in rails console in the form Contact.first.to_do_items.create!() and I have ActiveRecord::RecordInvalid Validation failed: contact must exist
Models:
class Contact < ApplicationRecord
acts_as_token_authenticatable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :to_do_items
end
class ToDoItem < ApplicationRecord
belongs_to :сontact
end
It works well when I'm using optional: true in the ToDoItem class but I'm curious why it's not creating a record with the code listed above.
I'm new in RoR. I'm learning models and generating the models for my app and it's associations. The console generated the error when I tried:
$ rails console
mypath.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.1.3/lib/active_record/associations.rb:1395:in
`has_many': wrong number of arguments (given 4, expected 1..3)
(ArgumentError)
and more lines of errors but this one is the key I think.
Solved it, I was writing on the same line different associations which is not correct:
Wrong:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :questions, :answers, :comments, :votes
end
Correct:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :questions
has_many :answers
has_many :comments
has_many :votes
end
I am new to stackoverflow and i want to implement user with multiple roles .
I had started with rolify gem . I had generated 3 devise users manager , owner , user (visitor).
Association used for my application is
class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource,
:polymorphic => true,
:optional => true
validates :resource_type,
:inclusion => { :in => Rolify.resource_types },
:allow_nil => true
scopify
end
class User < ApplicationRecord
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:visitor) if self.roles.blank?
end
end
class Owner < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:owner) if self.roles.blank?
end
end
class Manager < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:moderator) if self.roles.blank?
end
end
My concern is i am using rolify gem to assign role but i want to keep manager , owner , visitor table separate but if i didn't use Single table inheritance then how can i able to implement roles and keep table separate
I am working with devise, and I am trying to allow users to signup using twitter/facebook. I am very confused because I keep getting \
No route matches {:controller=>"authentications", :action=>"passthru", :provider=>:twitter, :format=>nil} missing required keys: [:provider]
Routes.rb
devise_for :users,controllers: {omniauth_callbacks: "authentications", registrations: "registrations"}
AuthenticationController.rb
class AuthenticationsController < ApplicationController
def index
#authentications = Authentication.all
end
def create
#authentication = Authentication.new(params[:authentication])
if #authentication.save
redirect_to authentications_url, :notice => "Successfully created authentication."
else
render :action => 'new'
end
end
def destroy
#authentication = Authentication.find(params[:id])
#authentication.destroy
redirect_to authentications_url, :notice => "Successfully destroyed authentication."
end
def twitter
raise omni = request.env["omniauth.auth"].to_yaml
end
end
I assume you've something like the below in the User model; because of this, you are getting this routing error.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :omniauthable,
:omniauth_providers => [:facebook],
:omniauth_providers => [:twitter]
Change it to the following:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :omniauthable,
:omniauth_providers => [:facebook, :twitter]
I was following the omniauth example on github and I had
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:google]
end
but needed to only have the one devise line as follows:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:google]
end