ruby on rails - How to login successfully in typo application? -
i able see output in heroku fixing errors in gemfile
also login admin...but 1 time...then password not recognized , login not successful... not save password on admin page(error:password doesn't match confirmation) , not login using admin password provided(error:login unsuccessful)
i've tried create admin , password manually got errors.
how login using admin credential?
thanks
errors , files related below
//errors
typo-1 (master) $ rails console loading development environment (rails 3.0.17) 1.9.3-p551 :001 > user.create(:username => 'admin', 1.9.3-p551 :002 > :password => 'abc123', 1.9.3-p551 :003 > :password_confirmation => 'abc123') activerecord::unknownattributeerror: unknown attribute: username /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1565:in `block in attributes=' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `each' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `attributes=' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1412:in `initialize' /home/ubuntu/workspace/typo-1/app/models/user.rb:53:in `initialize' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `new' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `create' (irb):1 /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:44:in `start' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:8:in `start' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands.rb:23:in `<top (required)>' script/rails:6:in `require' script/rails:6:in `<main>' 1.9.3-p551 :004 > user.create(:username => 'admin', 1.9.3-p551 :005 > :password_confirmation => 'abc123') activerecord::unknownattributeerror: unknown attribute: username /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1565:in `block in attributes=' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `each' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `attributes=' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1412:in `initialize' /home/ubuntu/workspace/typo-1/app/models/user.rb:53:in `initialize' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `new' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `create' (irb):4 /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:44:in `start' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:8:in `start' /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands.rb:23:in `<top (required)>' script/rails:6:in `require' script/rails:6:in `<main>' 1.9.3-p551 :006 >
//controllers/admin/users_controller.rb
class admin::userscontroller < admin::basecontroller cache_sweeper :blog_sweeper def index @users = user.order('login asc').page(params[:page]).per(this_blog.admin_display_elements) end def new @user = user.new @user.attributes = params[:user] @user.text_filter = textfilter.find_by_name(this_blog.text_filter) setup_profiles @user.name = @user.login if request.post? , @user.save flash[:notice] = _('user created.') redirect_to :action => 'index' end end def edit @user = params[:id] ? user.find_by_id(params[:id]) : current_user setup_profiles @user.attributes = params[:user] if request.post? , @user.save if @user.id = current_user.id current_user = @user end flash[:notice] = _('user updated.') redirect_to :action => 'index' end end def destroy @record = user.find(params[:id]) return(render 'admin/shared/destroy') unless request.post? @record.destroy if user.count > 1 redirect_to :action => 'index' end private def setup_profiles @profiles = profile.find(:all, :order => 'id') end end
//models/user.rb
require 'digest/sha1' # typo user. class user < activerecord::base include configmanager belongs_to :profile belongs_to :text_filter delegate :name, :to => :text_filter, :prefix => true delegate :label, :to => :profile, :prefix => true has_many :notifications, :foreign_key => 'notify_user_id' has_many :notify_contents, :through => :notifications, :source => 'notify_content', :uniq => true has_many :articles, :order => 'created_at desc' serialize :settings, hash # settings setting :notify_watch_my_articles, :boolean, true setting :editor, :string, 'visual' setting :firstname, :string, '' setting :lastname, :string, '' setting :nickname, :string, '' setting :description, :string, '' setting :url, :string, '' setting :msn, :string, '' setting :aim, :string, '' setting :yahoo, :string, '' setting :twitter, :string, '' setting :jabber, :string, '' setting :show_url, :boolean, false setting :show_msn, :boolean, false setting :show_aim, :boolean, false setting :show_yahoo, :boolean, false setting :show_twitter, :boolean, false setting :show_jabber, :boolean, false setting :admin_theme, :string, 'blue' # echo "typo" | sha1sum - class_attribute :salt def self.salt '20ac4d290c2293702c64b3b287ae5ea79b26a5c1' end attr_accessor :last_venue def initialize(*args) super self.settings ||= {} end def self.authenticate(login, pass) find(:first, :conditions => ["login = ? , password = ? , state = ?", login, password_hash(pass), 'active']) end def update_connection_time self.last_venue = last_connection self.last_connection = time.now self.save end # these create , unset fields required remembering users between browser closes def remember_me remember_me_for 2.weeks end def remember_me_for(time) remember_me_until time.from_now.utc end def remember_me_until(time) self.remember_token_expires_at = time self.remember_token = digest::sha1.hexdigest("#{email}--#{remember_token_expires_at}") save(:validate => false) end def forget_me self.remember_token_expires_at = nil self.remember_token = nil save(:validate => false) end def permalink_url(anchor=nil, only_path=false) blog = blog.default # remove me... blog.url_for( :controller => 'authors', :action => 'show', :id => login, :only_path => only_path ) end def self.authenticate?(login, pass) user = self.authenticate(login, pass) return false if user.nil? return true if user.login == login false end def self.find_by_permalink(permalink) self.find_by_login(permalink).tap |user| raise activerecord::recordnotfound unless user end end def project_modules profile.project_modules end # generate methods takes accesscontrol rules # example: # # def publisher? # profile.label == :publisher # end accesscontrol.roles.each |role| define_method "#{role.to_s.downcase}?" profile.label.to_s.downcase == role.to_s.downcase end end def self.to_prefix 'author' end def simple_editor? editor == 'simple' end def password=(newpass) @password = newpass end def password(cleartext = nil) if cleartext @password.to_s else @password || read_attribute("password") end end def article_counter articles.size end def display_name name end def permalink login end def to_param permalink end def admin? profile.label == profile::admin end protected # apply sha1 encryption supplied password. # additionally surround password salt # additional security. def self.password_hash(pass) digest::sha1.hexdigest("#{salt}--#{pass}--") end def password_hash(pass) self.class.password_hash(pass) end before_create :crypt_password # before saving record database crypt password # using sha1. # never store actual password in db. # before encryption, send email user can remind # password def crypt_password send_create_notification write_attribute "password", password_hash(password(true)) @password = nil end before_update :crypt_unless_empty # if record updated check if password empty. # if empty assume user didn't want change # password , reset old value. def crypt_unless_empty if password(true).empty? user = self.class.find(self.id) write_attribute "password", user.password else crypt_password end end before_validation :set_default_profile def set_default_profile if user.count.zero? self.profile ||= profile.find_by_label('admin') else self.profile ||= profile.find_by_label('contributor') end end validates_uniqueness_of :login, :on => :create validates_uniqueness_of :email, :on => :create validates_length_of :password, :within => 5..40, :if => proc.new { |user| user.read_attribute('password').nil? or user.password.to_s.length > 0 } validates_presence_of :login validates_presence_of :email validates_confirmation_of :password validates_length_of :login, :within => 3..40 private # send mail of creation user user create def send_create_notification begin email_notification = notificationmailer.notif_user(self) emailnotify.send_message(self, email_notification) rescue => err logger.error "unable send notification of create user email: #{err.inspect}" end end end
//editing: replacement user_id login
$ rails console loading development environment (rails 3.0.17) 1.9.3-p551 :001 > user.create(login: 'admin', password: 'password123', password_confirmation: 'password123') => #<user id: nil, login: "admin", password: nil, email: nil, name: nil, notify_via_email: nil, notify_on_new_articles: nil, notify_on_comments: nil, profile_id: 3, remember_token: nil, remember_token_expires_at: nil, text_filter_id: "1", state: "active", last_connection: nil, settings: {}> 1.9.3-p551 :002 > exit
according error message don't have username
field in user
model.
activerecord::unknownattributeerror: unknown attribute: username
i think using attribute login
in model. try replace username
login
, see if works.
user.create(login: 'admin', password: 'password123', password_confirmation: 'password123')
or can post schema user model , can have look.
Comments
Post a Comment