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

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

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

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

Ruby

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

Q&A

1回答

1140閲覧

Railsで「No route matches [GET] "/contacts/confirm"」とエラーが出る

Shmupeiii

総合スコア105

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2022/04/01 09:44

こちらの記事(https://qiita.com/japwork/items/145645e281b81d9bf92c)
を参考にしながら、お問合せフォームを作成しています。
一通り作成し終えて、作成した contacts/confirm にアクセスしようと思ったんですが、以下のようなエラーが出ました。

No route matches [GET] "/contacts/confirm" Rails.root: /app Application Trace | Framework Trace | Full Trace actionpack (6.1.4.6) lib/action_dispatch/middleware/debug_exceptions.rb:33:in `call' web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app' web-console (4.2.0) lib/web_console/middleware.rb:19:in `block in call' web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch' web-console (4.2.0) lib/web_console/middleware.rb:17:in `call' actionpack (6.1.4.6) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call' railties (6.1.4.6) lib/rails/rack/logger.rb:37:in `call_app' railties (6.1.4.6) lib/rails/rack/logger.rb:26:in `block in call' activesupport (6.1.4.6) lib/active_support/tagged_logging.rb:99:in `block in tagged' activesupport (6.1.4.6) lib/active_support/tagged_logging.rb:37:in `tagged' activesupport (6.1.4.6) lib/active_support/tagged_logging.rb:99:in `tagged' railties (6.1.4.6) lib/rails/rack/logger.rb:26:in `call' sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call' actionpack (6.1.4.6) lib/action_dispatch/middleware/remote_ip.rb:81:in `call' actionpack (6.1.4.6) lib/action_dispatch/middleware/request_id.rb:26:in `call' rack (2.2.3) lib/rack/method_override.rb:24:in `call' rack (2.2.3) lib/rack/runtime.rb:22:in `call' activesupport (6.1.4.6) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' actionpack (6.1.4.6) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (6.1.4.6) lib/action_dispatch/middleware/static.rb:24:in `call' rack (2.2.3) lib/rack/sendfile.rb:110:in `call' actionpack (6.1.4.6) lib/action_dispatch/middleware/host_authorization.rb:119:in `call' rack-mini-profiler (2.3.4) lib/mini_profiler/profiler.rb:393:in `call' webpacker (5.4.3) lib/webpacker/dev_server_proxy.rb:25:in `perform_request' rack-proxy (0.7.2) lib/rack/proxy.rb:67:in `call' railties (6.1.4.6) lib/rails/engine.rb:539:in `call' puma (5.6.2) lib/puma/configuration.rb:252:in `call' puma (5.6.2) lib/puma/request.rb:77:in `block in handle_request' puma (5.6.2) lib/puma/thread_pool.rb:340:in `with_force_shutdown' puma (5.6.2) lib/puma/request.rb:76:in `handle_request' puma (5.6.2) lib/puma/server.rb:441:in `process_client' puma (5.6.2) lib/puma/thread_pool.rb:147:in `block in spawn_thread'

正しくルーティングが設定されていると思ったんですが、理由が分かる方がいれば教えて頂きたいです。

Ruby

1config/routes.rb 2Rails.application.routes.draw do 3 get 'mypage', to: 'users#me' 4 get 'user_edit', to: 'users#edit' 5 post 'login', to: 'sessions#create' 6 delete 'logout', to: 'sessions#destroy' 7 get 'about', to: 'application#about' 8 get 'contact', to: 'application#contact' 9 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 10 root 'home#index' 11 # root 'boards#index' 12 resources :users, only: %i[new create edit update show] 13 resources :boards 14 resources :comments, only: %i[create destroy] 15 16#該当箇所 17 resources :contacts, only: [:new, :create] 18 post 'contacts/confirm', to: 'contacts#confirm', as: 'confirm' 19 post 'contacts/back', to: 'contacts#back', as: 'back' 20 get 'done', to: 'contacts#done', as: 'done' 21end 22 23

Ruby

1#views/contacts/confirm.html.erb 2<table> 3 <tbody> 4 <tr> 5 <td class="text-center" style="width: 30%;">お名前</td> 6 <td><%= @contact.name %></td> 7 </tr> 8 <tr> 9 <td class="text-center">メッセージ</td> 10 <td style="white-space: pre-wrap;"><%= @contact.message %></td> 11 </tr> 12 </tbody> 13</table> 14 15<%= form_for(@contact) do |f| %> 16 <%= f.hidden_field :name %> 17 <%= f.hidden_field :email %> 18 <%= f.hidden_field :phone_number %> 19 <%= f.hidden_field :subject %> 20 <%= f.hidden_field :message %> 21 <div><%= f.submit '送信' %></div> 22<% end %> 23<%= form_for @contact, url: back_path do |f| %> 24 <%= f.hidden_field :name %> 25 <%= f.hidden_field :email %> 26 <%= f.hidden_field :phone_number %> 27 <%= f.hidden_field :subject %> 28 <%= f.hidden_field :message %> 29 <div><%= f.submit '入力画面に戻る' %></div> 30<% end %>

Ruby

1#contacts_controller.rb 2class ContactsController < ApplicationController 3 def new 4 @contact = Contact.new 5 end 6 7 # 確認画面を作成する場合はこのような記述になるかと思います。 8 # newアクションから入力内容を受け取り、 9 # 送信ボタンを押されたらcreateアクションを実行します。 10 def confirm 11 @contact = Contact.new(contact_params) 12 if @contact.invalid? 13 render :new 14 end 15 end 16  17 # 入力内容に誤りがあった場合、 18 # 入力内容を保持したまま前のページに戻るのが当たり前になっているかと思いますが、 19 # backアクションを定義することで可能となります。 20 def back 21 @contact = Contact.new(contact_params) 22 render :new 23 end 24 25 # 実際に送信するアクションになります。 26 # ここで初めて入力内容を保存します。 27 # セキュリティーのためにも一定時間で入力内容の削除を行ってもいいかもしれません。 28 def create 29 @contact = Contact.new(contact_params) 30 if @contact.save 31 ContactMailer.send_mail(@contact).deliver_now 32 redirect_to done_path 33 else 34 render :new 35 end 36 end 37 38 # 送信完了画面を使用する場合お使いください。 39 def done```ここに言語を入力 40コード

end

private

def contact_params
params.require(:contact)
.permit(:email,
:name,
:phone_number,
:subject,
:message
)
end
end

```Ruby #gemfile source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.7.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.4', '>= 6.1.4.6' # Use mysql as the database for Active Record gem 'mysql2', '~> 0.5' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets gem 'sass-rails', '>= 6' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 5.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.4', require: false gem 'jquery-rails' gem 'bootstrap', '~>4.0.0' gem 'kaminari' 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 'rspec-rails', '~> 3.8' gem 'rails-controller-testing' gem 'rails-flog', require: 'flog' end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 4.1.0' # Display performance information such as SQL time and flame graphs for each request in your browser. # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md gem 'rack-mini-profiler', '~> 2.0' gem 'listen', '~> 3.3' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 3.26' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

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

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

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

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

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

guest

回答1

0

ブラウザーから urlを入力してアクセスしたのでしょうか。GET になっています。routes での定義は POST です。

その方法では GET になっていまいます。 viewを書いてそのlinkから行くか、
methodを指定できる curl などで試してください

投稿2022/04/01 13:17

winterboum

総合スコア23331

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

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

Shmupeiii

2022/04/04 11:29

コメントありがとうございます。 viewを書いてそのlinkから行くか、methodを指定できる curl などで試してください。とあったんですが、pathを指定する方法とcontacts/confirmからアクセスする方法しか調べても分からず詰まっている感じです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問