简体   繁体   中英

Rails Website Login not working in production (Heroku)

I am creating a Rails application containing a Login. In development everything is fine. Login in production (Heroku) fails.

As I am new to RoR I follow this Basic Tutorial . The App contains a "User" model. The user has a boolean attribute "activated" as an account needs to be activated after the signup. During login there is an statement (see sessions_controller.rb):

if user.activated?

which is true in development and false in production.

I pushed the database with seeds.rb. On other pages I can see content, also initialized with seeds.rb, so I think it is no general database issue.

As the databese seems to be okay and it runs in development I assume I just missed something, which is hard to detect for me as a newbie and hopefully easy to detect for an experienced programmer.

Here is a part of my code. Say if you need more.

sessions_controller.rb:

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
  if user.activated?
    log_in user
    params[:session][:remember_me] == '1' ? remember(user) : forget(user)
    redirect_to changenews_path
  else
    message  = "Account not activated. "
    message += "Check your email for the activation link."
    flash[:warning] = message
    redirect_to root_url
  end
else
  flash.now[:danger] = 'Invalid email/password combination'
  render 'new'
end
end

This checks if the user who is logging in is already activated. In production it leads to "else > message [...]".

my Gemfile:

source 'https://rubygems.org'

gem 'rails',        ' 5.1.6'
gem 'bootstrap', ' 4.2.1'
gem 'puma',         ' 3.9.1'
gem 'sass-rails',   ' 5.0.6'
gem 'uglifier',     ' 3.2.0'
gem 'coffee-rails', ' 4.2.2'
gem 'jquery-rails', ' 3.1.0'
gem 'turbolinks',   ' 5.0.1'
gem 'jbuilder',     ' 2.7.0'
gem 'coffee-script-source', ' 1.8.0'
gem 'bcrypt',         ' 3.1.12'
gem 'popper_js', ' 1.14.5'
gem 'font-awesome-sass', ' 5.8.0'

group :development, :test do
  gem 'sqlite3', ' 1.3.13'
  gem 'byebug',  ' 9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           ' 3.5.1'
  gem 'listen',                ' 3.1.5'
  gem 'spring',                ' 2.0.2'
  gem 'spring-watcher-listen', ' 2.0.1'
end

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest',                 '5.10.3'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

I tried to set all gems to the latest version ('>= XYZ') and do "bundle install", but there was no effect.

and the Heroku log (extract):

:23.352114+00:00 app[web.1]: I, [:23.352005 #4]  INFO -- : [shortened] Started POST "/login" for 77.183.95.212 at 2019-04-09 11:43:23 +0000

:23.353232+00:00 app[web.1]: I, [:23.353123 #4]  INFO -- : [shortened] Processing by SessionsController#create as HTML

:23.353509+00:00 app[web.1]: I, [:23.353266 #4]  INFO -- : [shortened]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"shortened", "session"=>{"email"=>"mymail@mymail.de", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}

:23.356478+00:00 app[web.1]: D, [:23.356409 #4] DEBUG -- : [shortened]   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2[0m  [["email", "mymail@mymail.de"], ["LIMIT", 1]]

:23.432392+00:00 app[web.1]: I, [:23.432248 #4]  INFO -- : [shortened] Redirected to https://myappname.herokuapp.com/

I can't get any information from the debug message. And i I have no clue what the "LIMIT" stands for. I hope someone of you knows an answer to my problem.

So usually I expect the login to be successful, as the database seems to be okay. Currently user.activated? ist false in Heroku.

Thanks in advance!

  1. Heroku runs bundle install when you push the code so you dont need to run this.

  2. Did you run heroku run db:seed to actually seed your database?

  3. If you did, you can run heroku run rails c , then u = User.all and manually check by looking at the output that the users have correctly been seeded.

  4. (opinion) I would suggest using the "devise" gem for users/logins etc.. but feel free to use whatever you are more confortable with.

  5. Please use the heroku logs to give us (and yourself) a better understanding of what is wrong by running heroku logs -n 200 , 200 being the number of lines to display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM