0

我已经按照这个要点在 Cloud9 上的 Rails 5 应用程序中设置了一个简单的授权https://github.com/equivalent/scrapbook2/blob/master/archive/blogs/2016-09-simple-ralis-authentication-for-one-user .md

它在 Cloud9 上完美运行,但是当我部署到 Heroku 而不是登录屏幕时,我收到 404 错误。

地址栏显示正确的地址:

https://phone-storage-app.herokuapp.com/login

但我被重定向到标准 rails 404 页面,说我正在寻找的页面不存在。

如果我尝试,结果相同:

https://phone-storage-app.herokuapp.com/sessions/new

一开始,转到https://phone-storage-app.herokuapp.com Heroku 日志显示:

heroku[router]: at=info method=GET path="/" host=phone-storage-app.herokuapp.com request_id=23217153-3c94-483d-957d-c3b70e9212b7 fwd="91.215.176.16" dyno=web.1 connect=2ms service=76ms status=302 bytes=900
heroku[router]: at=info method=GET path="/login" host=phone-storage-app.herokuapp.com request_id=cd28c3ff-2b00-4774-b3a5-8230d758e7d4 fwd="91.215.176.16" dyno=web.1 connect=1ms service=147ms status=404 bytes=1750

环境变量设置正确,因为 AWS S3 连接运行良好,一旦我删除授权。

尝试预编译资产 - 结果相同。

现在已经呆了 3 天了,真的很感激任何想法!

该应用程序必须非常轻巧,因此 Devise 和其他身份验证引擎不会是首选路线。

模型/site_user.rb

class SiteUser
 include ActiveModel::Model
 attr_accessor :username, :password
 def login_valid?
   username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASS']
 end
end

控制器/session_controller.rb

class SessionsController < ApplicationController

   def new
     @site_user = SiteUser.new
   end

   def create
    sleep 2
    site_user_params = params.require(:site_user)

    @site_user = SiteUser.new
     .tap { |su| su.username = site_user_params[:username] }
     .tap { |su| su.password = site_user_params[:password] }

    if @site_user.login_valid?
      session[:current_user] = true
      flash[:notice] = 'Welcome back!'
      redirect_to '/contacts'
    else
      @site_user.password = nil
      flash[:error] = 'Sorry, wrong credentils'
      render 'new'
    end
  end

  def destroy
    reset_session
    redirect_to root_path
  end
end

控制器/application_controller.rb

class ApplicationController < ActionController::Base
 add_flash_types :notice, :error
 protect_from_forgery with: :exception
 ApplicationNotAuthenticated = Class.new(Exception)
 rescue_from ApplicationNotAuthenticated do
   respond_to do |format|
    format.json { render json: { errors: [message: "401 Not Authorized"] }, status: 401 }
    format.html do
       flash[:error] = "You are not authorized to access this page, please log in"
       redirect_to '/login'
    end
     format.any { head 401 }
   end
 end

  def authentication_required!
    session[:current_user] || raise(ApplicationNotAuthenticated)
  end
end

尝试切换到 StandardError 继承:

ApplicationNotAuthenticated = Class.new(StandardError)

问题仍然存在。

路线.rb

Rails.application.routes.draw do
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  get 'lists/index'
  resources :lists

  get 'contacts/index'
  get 'contacts/download'
  get 'contacts/create_list'
  get 'contacts/search'
  get 'contacts/process_file'
  resources :contacts do
   collection do
    post 'process_file'
    post 'save_list'
    post 'load_to_s3'
    get 'search'
    post 'create_list'
   end
  end

    root to: "contacts#index"
 end

意见/会话/new.html.rb

<div style="margin-top:70px">
  <section>
   <%= form_for(:site_user, url: login_path) do |f| %>
    <div>
      <%= f.label :username %>
      <%= f.text_field :username %>
    </div>
    <div>
      <%= f.label :password %>
      <%= f.password_field :password %>
    </div>
    <br />
    <div>
      <%= f.submit 'Log in' %>
    </div>
  <% end %>
 </section>
</div>

更新 1

安装 gem traceroute。下面是运行它的结果:

云9

Unused routes (9):
contacts#create
contacts#new
contacts#edit
contacts#show
contacts#update
contacts#update
sessions#new
sessions#create
sessions#destroy

Heroku

Unused routes (6):
contacts#create
contacts#new
contacts#edit
contacts#show
contacts#update
contacts#update

忘记未使用路由的事实,它告诉我 Heroku 上存在某种路由问题。这可能与存在没有控制器的 SiteUser 模型和没有模型的 SessionsController 的事实有关吗?或者它是一个没有人知道的错误?

更新 2

以“开发”模式在 Heroku 上运行应用程序。完全糊涂了!获取“路由错误未初始化常量 SessionsController”。虽然路线显然是正确的。

路由错误

以“生产”模式在 Cloud9 上运行应用程序。一切都很完美。

4

1 回答 1

0

我花了 4 天时间,但问题出乎意料。Git 没有同步所需的控制器文件。

$ git commit -am "commit 4"
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
    app/assets/javascripts/sessions.coffee
    app/assets/stylesheets/sessions.scss
    app/controllers/sessions_controller.rb
    app/helpers/sessions_helper.rb
    app/models/site_user.rb
    app/views/sessions/
    db/migrate/20170224183940_create_site_users.rb
    test/controllers/sessions_controller_test.rb
    test/fixtures/site_users.yml
    test/models/site_user_test.rb

nothing added to commit but untracked files present

决议是运行 $ git add .

然后像往常一样提交。

于 2017-02-25T21:44:24.503 回答