質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

Q&A

解決済

2回答

524閲覧

search.html.erbを表示させたいがルーティングエラーが解決できない

masakingu

総合スコア11

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

0グッド

0クリップ

投稿2022/11/03 13:02

前提

RailsでAPIを利用してサッカー選手を検索できるアプリを作成しています。
search.html.erbを表示させたいがルーティングエラーが出ます。
原因がわからず困っています。宜しくお願いします

実現したいこと

search.html.erbを表示させたいです。

ここに実現したいことを箇条書きで書いてください。

  • ▲▲機能を動作するようにする

発生している問題・エラーメッセージ

ターミナルのエラーメッセージです。

Started GET "/players/serach" for ::1 at 2022-11-03 21:36:41 +0900 ActionController::RoutingError (No route matches [GET] "/players/serach"):

実現したいこと

search.html.erbを表示させたいです。

発生している問題・エラーメッセージ

ターミナルのエラーメッセージです。

Started GET "/players/serach" for ::1 at 2022-11-03 21:36:41 +0900 ActionController::RoutingError (No route matches [GET] "/players/serach"):

該当のソースコード

search.html.erb

<%= form_with url: players_search_path, method: :get, local: true do |form| %> <%= form.text_field :season, class: 'form-control', placeholder: '西暦を入力してください(例: 1997)' %> <div class="input-group mb-3"> <%= form.text_field :player_name, class: 'form-control', placeholder: '選手名を入力してください' %> <div class="input-group-append"> <%= f.submit '検索', class: 'btn btn-primary' %> </div> </div> </div> <% end %>

players_controller.rb

class PlayersController < ApplicationController require 'uri' require 'net/http' require 'openssl' require 'json' before_action :set_q, only: [:search] # GET /players or /players.json def index @players = Player.all end def search url = URI('https://api-football-v1.p.rapidapi.com/v3/players?team=85&search=cavani&season=2018') http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-rapidapi-host"] = 'api-football-v1.p.rapidapi.com' request["x-rapidapi-key"] = 'ENV['API_KEY']' response = http.request(request) puts response.read_body #@q = Player.ransack(params[:q]) #@players = @q.result(distinct: true).includes(:user).order(created_at: :desc).page(params[:page]) #@seasons = @q.result(distinct: true).includes(:user).order(created_at: :desc).page(params[:page]) end private def set_q @q = User.ransack(params[:q]) end # Use callbacks to share common setup or constraints between actions. def set_player @player = Player.find(params[:id]) end # Only allow a list of trusted parameters through. def player_params params.require(:player).permit(:player_name, :season) end def search_params params[:q]&.permit(:player_name, :season) end end

routes.rb

Rails.application.routes.draw do root 'static_pages#top' get 'login', to: 'user_sessions#new' post 'login', to: 'user_sessions#create' delete 'logout', to: 'user_sessions#destroy' resources :users, only: %i(new create) # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html resources :players, only: %i[index] do collection do get 'search' end end end

rails routes

Prefix Verb URI Pattern Controller#Action root GET / static_pages#top login GET /login(.:format) user_sessions#new POST /login(.:format) user_sessions#create logout DELETE /logout(.:format) user_sessions#destroy users POST /users(.:format) users#create new_user GET /users/new(.:format) users#new search_players GET /players/search(.:format) players#search players GET /players(.:format) players#index rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy new_rails_conductor_inbound_email_source GET /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format) rails/conductor/action_mailbox/inbound_emails/sources#new rails_conductor_inbound_email_sources POST /rails/conductor/action_mailbox/inbound_emails/sources(.:format) rails/conductor/action_mailbox/inbound_emails/sources#create rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create rails_service_blob GET /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show rails_service_blob_proxy GET /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show rails_blob_representation GET /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show rails_blob_representation_proxy GET /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/proxy#show GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create

補足情報(FW/ツールのバージョンなど)

Rails バージョン 6.1.5
宜しくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

/players/serach ってどこから出てきたのだろう?? 載ってる codeではみな search なのに。エラーメッセージ編集してません?

気になるのは
rails routes では search_players views では players_search

投稿2022/11/06 08:15

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

/players/searchではなく/players/serachにアクセスしています。

投稿2022/11/03 13:09

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

masakingu

2022/11/07 03:54

見落としてました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問