我已經安裝在CLOUD9 Rails的5應用一個簡單的授權下這個要點https://github.com/equivalent/scrapbook2/blob/master/archive/blogs/2016-09-simple-ralis-authentication-for-one-user.mdRails應用程序失敗做顯示登錄屏幕
它完美的CLOUD9,但是當我部署到Heroku的替代登錄屏幕我得到404錯誤。
地址欄顯示正確的地址:
https://phone-storage-app.herokuapp.com/login
,但我重定向到標準導軌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)
問題依然存在。
的routes.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>
UPDATE 1個
安裝寶石跟蹤路由。下面是運行它的結果:
CLOUD9
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?或者是沒有人知道的一些錯誤?
UPDATE 2
然在Heroku在App在 '發展' 模式。完全困惑!獲取「路由錯誤未初始化的常量SessionsController」。雖然路線顯然是正確的。
冉上CLOUD9在App在 '生產' 模式。一切都很完美。