rubyでオリジナルアプリを作っているのですがherokuでデプロイしたアプリのTwitter認証機能(Twitterアカウントでログインする機能)を使おうとすると「◯◯(アプリ名)にアカウントへのアクセスを許可しますか?」やその次の「アプリケーションに戻ります」のページには行けるのですがそこから元のアプリケーションに戻れず
The page you were looking for doesn't exist.
というエラーが出てしまいます
エラーの原因がわからないためheroku logを見てみたところ
ActionController::RoutingError (No route matches [GET] "/users"):
というエラーが出ていました
ローカル環境では問題なく動いていて本番環境でもこのTwitter認証の部分だけこのエラーが発生しています
Rubyのバージョンは2.5.0、Railsのバージョンは5.2.4.1です
発生している問題・エラーメッセージ
The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved.
ActionController::RoutingError (No route matches [GET] "/users"):
該当のソースコード
**route.rb** Rails.application.routes.draw do get "/" => "home#top" post "login" => "users#login" #OK get "login" => "users#login_form" #OK post "logout" => "users#logout" post "users/:id/update" => "users#update" get "users/:id/edit" => "users#edit" post "users/create" => "users#create" #OK get "signup" => "users#new" #OK get "users/:id" => "users#show" get "posts/index" => "posts#index" get "posts/new" => "posts#new" get "posts/:id" => "posts#show" post "posts/create" => "posts#create" get "posts/:id/edit" => "posts#edit" post "posts/:id/update" => "posts#update" post "posts/:id/destroy" =>"posts#destroy" #OK get "about" => "home#about" #OK # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html get 'auth/:provider/callback' => 'users#creates'#このpathを通して認証が行われる。 end
**Gemfile** source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.4' # Use sqlite3 as the database for Active Record gem 'sqlite3', group: :development gem 'pg', group: :production # Use Puma as the app server gem 'puma', '~> 3.12' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' gem 'bcrypt' gem 'omniauth' gem 'omniauth-twitter' gem 'dotenv-rails' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'pry-rails' end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
**controllers/users_controller.rb** class UsersController < ApplicationController before_action :authenticate_user,{only:[:index,:show,:edit,:update]} before_action :forbid_login_user,{only:[:new,:create,:login_form,:login]} before_action :ensure_correct_user,{only:[:edit,:update]} def show @user = User.find_by(id: params[:id]) @post = Post.where(user_id: @user.id) end def new @user = User.new end def create @user = User.new( name: params[:name], email: params[:email], image_name: "pile-up3.jpg", password: params[:password] ) if @user.save session[:user_id] = @user.id redirect_to("/users/#{@user.id}") else render("users/new") end end #twitter認証の def creates @user = User.new( name: params[:name], email: params[:email], image_name: "pile-up3.jpg", password: params[:password] ) user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])#request.env['omniauth.auth']はTwitter認証で得た情報を格納するもの if user session[:user_id] = user.id redirect_to("/users/#{user.id}") else redirect_to root_path end end #twitter認証の def edit @user = User.find_by(id: params[:id]) end def update @user = User.find_by(id: params[:id]) @user.name = params[:name] @user.email = params[:email] if params[:image] @user.image_name = "#{@user.id}.jpg" image = params[:image] File.binwrite("public/user_images/#{@user.image_name}",image.read) end if @user.save redirect_to("/users/#{@user.id}") else render("users/edit") end end def login_form end def login @user = User.find_by(email: params[:email]) if @user && @user.authenticate(params[:password]) session[:user_id] = @user.id redirect_to("/users/#{@user.id}") else @error_message = "メールアドレスまたはパスワードが間違っています" @email = params[:email] @password = params[:password] render("users/login_form") end end def logout session[:user_id] = nil redirect_to("/") end def ensure_correct_user if @current_user.id != params[:id].to_i flash[:notice] = "権限がありません" redirect_to("/posts/index") end end end
**posts/index.html.erb** <div class="main-wrapper"> <div class="cont"> <div class="main-wrapper-main"> <div class="main-title"> <h1>ピラプ</h1> <p>あなたの頑張りを可視化するアプリケーション</p> </div> <div class="main-btn"> <% if @current_user %> <a class="main-btn1" href="/posts/new" >新規投稿</a> <a class="main-btn2" href="/users/<%= @current_user.id %>" >マイページ</a> <% else %> <a class="main-btn1" href="/signup" >新規登録はこちらから</a> <a class="main-btn2" href="/login" >メールアドレスでログイン</a> <a class="main-btn3" href="/auth/twitter" >Twitterでログイン</a> <% end %> </div> </div> </div> </div>
**secrets.yml** default_twitter: &default_twitter twitter_api_key: <%= ENV["TWITTER_CONSUMER_KEY"] %> twitter_api_secret: <%= ENV["TWITTER_CONSUMER_SECRET"] %> development: secret_key_base: <<: *default_twitter test: secret_key_base: <<: *default_twitter production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> twitter_api_key: <%= ENV["TWITTER_CONSUMER_KEY"] %> twitter_api_secret: <%= ENV["TWITTER_CONSUMER_SECRET"] %>
**omniauth.rb** Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET'], callback_url: "https://https://pile-up.herokuapp.com//users/auth/twitter/callback" end
試したこと
このエラーはルートに問題がある際に起こるエラーなのだと思うのですが、上記のコードからも分かるとおり
get "users"
というようなルートは設定していないので正直なにが問題でこのエラーが発生しているのかよくわかりません
The page you were looking for doesn't exist.
の方で調べてみたところ
get "/" => "home#top"
や
root 'users#index'
がなかったため起こったエラーで上記のものを入れたら解決したという例や上記のルートをroute.rbの一番上に書き換えたら解決できたという例があったので試してみたのですが解決できませんでした
この問題の解決方法がお分かりになる方がいらっしゃいましたらアドバイスお願いします