I'm working on letting users sign in and out of my rails app. The error I get is as follows
undefined method 'find_by_remember_token'
The method in question is written like this:
def current_user
#current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
Any help you can provide in fixing this error would be greatly appreciated!
you can reset your database and migrate in one line:
rake db:migrate:reset && rake db:migrate && annotate
use the gem annotate in your project to have a better view of your database columns
in your Gemfile add:
gem 'annotate'
and in console run:
bundle update && bundle install
You don't have a field in your users table called remember_token ?
I know this is an old question, but I had the same problem and wanted to include how I fixed it.
First, I made sure I added the required info to the migration:
class AddRememberTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :remember_token, :string
add_index :users, :remember_token
end
end
then I just dumped my db and remigrated:
rake db:drop
rake db:create
rake db:migrate
worked for me after that.
Related
I am a brand new Ruby on Rails User
When I enter localhost:3000 ,
I get an error reading
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
You have 2 pending migrations:
20221119205559_add_devise_to_users.rb
20221119211811_drop_users_table.rb
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
end
def load_schema_if_pending!
In a nutshell, it was working before but I attempted to create a a basic authentication page(which was also working) , but for some reason when I clicked sign up I received an error also.
Thank you for any tips on how to fix this!
I have tried to
rake db:drop
rake db:create
rake db:migrate
Editing the migrate file with:
edit your migration file
class DropUsersTable < ActiveRecord::Migration
def change
drop_table :users
end
end
Then doing rake db:migrate
Also, I have run :
rails generate devise:install
rails generate devise User
bundle exec rake db:migrate
I've been trying to get the Paperclip gem working. The problem that I was initially running into was that pictures were getting uploaded but not displaying. I then messed around with the database by doing a rake db:rollback to try and fix the error. Now I can't rake db:migrate again because of this error
SQLite3::SQLException: duplicate column name: image_file_name: ALTER TABLE "posts" ADD "image_file_name" varchar
I've personally went into the migration folder and deleted the file to try and generate a migration again. I've been trying to do rails generate paperclip post image and it does create a migration file, but I'm unable to rake db:migrate.
Any suggestions?
Thanks!
Deleting a migration file doesn't really rollback the change it made in your database. Your best bet is to:
Don't delete the migration but comment out the content of the migration class, and run rake db:migrate,
Let's say I have this as my migration file
class AddEmailSentToNeeds < ActiveRecord::Migration
def change
add_column :needs, :email_sent, :boolean ,default: false
end
end
Just comment out the method but leave the class, so it would be:
class AddEmailSentToNeeds < ActiveRecord::Migration
# def change
# add_column :needs, :email_sent, :boolean ,default: false
# end
end
This is just a hacky way to tell rails to skip this migration.
OR
start from the start so go do, rake db:drop, rake db:create, and rake db:migrate
I defined a model in rails using command rails generate model testdetails . After that I went to db/migrate and wrote the fields for this model
class CreateTestDetails < ActiveRecord::Migration
def self.up
create_table :test_details do |t|
t.column :TestName ,:string
t.column :TestType ,:integer
end
end
end
then i did db:migrate , it throws some error . I google it and found out that it may be coming due to devise version ( I am using devise for authentication) , I updated Gemfile and wrote the version of devise (2.1) and did bundle install . After that I again did db : migrate but it is showing this error
rake aborted!
undefined method secret_key=' for Devise:Module
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:7:inblock in '
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:3:in <top (required)>'
/home/vibhor/rails_projects/recruit/config/environment.rb:5:in'
Tasks: TOP => db:migrate => environment
what should i do so that this model can be created without any error? i am using rails 3.2.13 and ruby 2.0.0
I think this is due to gem version so update it to latest version 3.x or remove that line from your config/initializers/devise.rb file.
In your config/initializers/devise.rb file add this line:
config.secret_key = 'Your secret Key'
And use rake secret to generate your secret key.
There's an issue open in github if you need more info.
Had the same problems lately. Check devise initializers, as it has changed lately. For me it solved the problem.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps
end
# Create sample User
User.create :name => "TestUser"
end
end
When run rake db:create and rake db:migrate command
In development database it creates sample user but not in test database. I want this user in test database also. any reason?
Also tried rake db:migrate RAILS_ENV=test.
First you should run the following command rake db:test:prepare in order to "prepare" the test database.
Finally, I recommend using the following file db/seeds.rb if you wish to add new records in your database.
In the db/seeds.rb file:
User.create(name: "TestUser")
Run:
rake db:seed
rake db:seed RAILS_ENV=test # for test env
If you're looking to test a user why not create a fixture or use rspec for testing something like this? The above code is bad practice. I would do something like this
migration file.
def self.up
create_table :users do |t|
t.string :name
t.timestamps
end
end
Depending on your testing stack I would setup a fixture like so(yml fixture)
david:
name: David Smith
You could also use rspec with factory girl which makes testing alot easier in rails.
From this morning, I am facing weird issues with Rails devise. Following is output of my ls and rake db version command.
hrishikesh#hrishikesh-ubuntu:~/git-public/personaldiary/db/migrate$ ls -1
20120110083934_devise_create_users.rb
20120110090514_create_posts.rb
20120110090845_add_user_id_to_post.rb
20120203035323_add_confirmable_to_devise.rb
20120203035323_add_confirmable_to_devise.rb~
20120203043601_add_lockable_to_devise.rb
20120203043601_add_lockable_to_devise.rb~
hrishikesh#hrishikesh-ubuntu:~/git-public/personaldiary/db/migrate$ rake db:version
(in /home/hrishikesh/git-public/personaldiary)
DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /usr/lib/ruby/vendor_ruby/activerecord.rb:2)
Current version: 20120203034555
hrishikesh#hrishikesh-ubuntu:~/git-public/personaldiary/db/migrate$
If I try to add any new migrations, rake db:migrate throws error that tells me that some column already exists, and fails.
My failing migration code is here:
class AddConfirmableToDevise < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
I specifically do not want to use up and down methods because of this
Please help.
After spending hours to find solution, I decided to give up and ran
rake db:migrate:reset
And it worked, only thing is my data was lost, which was not that big deal at this point.
Thank you everyone for attempting to solve this.