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

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

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

GETとはHTTPが対応するリクエストメソッドの一つです。クライアントからサーバーへ送られたURLパラメータのデータを取得する時必要がある時に使われます。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Ruby on Rails

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

ルーティング

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

Q&A

1回答

4243閲覧

[Rails]HTTPリクエストメソッドについて、GETメソッドをPOSTメソッドにしたいです。ご教示お願いします、、

suirunakamura

総合スコア16

GET

GETとはHTTPが対応するリクエストメソッドの一つです。クライアントからサーバーへ送られたURLパラメータのデータを取得する時必要がある時に使われます。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Ruby on Rails

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

ルーティング

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

0グッド

0クリップ

投稿2020/08/09 03:49

記事投稿機能の実装をしているところ、Webブラウザ上にて下記のようなエラーが表示されてしまいました。
https://gyazo.com/44a6cc57502052dcd62e2347ada801f0

Routing Error No route matches [POST] "/post/new" Rails.root: /Users/apple/create/portfolio Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom

これは、ルーティングのエラーで、本来は、POSTメソッドでなきゃいけないものを、GETメソッドになっていてデータが送れない?というエラーだと仮説しています。
rails routesコマンドを実行し、ルーティングを確認してみると下記のような表示になります。

rooter:portfolio apple$ rails routes Prefix Verb URI Pattern Controller#Action root GET / top#index post_index GET /post(.:format) post#index POST /post(.:format) post#create new_post GET /post/new(.:format) post#new edit_post GET /post/:id/edit(.:format) post#edit post GET /post/:id(.:format) post#show PATCH /post/:id(.:format) post#update PUT /post/:id(.:format) post#update DELETE /post/:id(.:format) post#destroy 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 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/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#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

このターミナルの表示をみてみると、postのnewアクションよりも先にcreateアクションがきてしまっています。今回のエラーはおそらくnewアクション内(GETメソッド)で記事投稿のフォーム内容の登録を行ってしまっていることにあるので、これをcreateアクション内(POSTメソッド)で行えるようにしたいです。

POSTコントローラーファイルを載せておきます。。

class PostController < ApplicationController class Post def new # ストロングパラメータから精査されたデータだけをインスタンスに格納 @blog = Post.new(blogs_params) # インスタンスに保存成功した場合の処理 if @blog.save flash[:success] = "ユーザーを登録しました" redirect_to @blog # インスタンスの保存に失敗した場合の処理 else flash[:danger] = "ユーザーの登録に失敗しました" render :new end end def create end private def blogs_params params.require(:articles).permit(:dating, :title, :text) end end end

この問題の確認・返信・ご教示どうかよろしくお願いします。。

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

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

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

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

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

guest

回答1

0

本来は、POSTメソッドでなきゃいけないものを、GETメソッドになっていてデータが送れない?というエラーだと仮説しています。

違います。POSTメソッドによるルーティングが存在しないところに、POSTが送信されているためのエラーです。

これをcreateアクション内(POSTメソッド)で行えるようにしたいです。

これはそのとおりです。

投稿2020/08/09 07:41

maisumakun

総合スコア145183

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

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

maisumakun

2020/08/09 07:42

ビューなど情報が不足していてコメントしづらい状況です。
suirunakamura

2020/08/09 08:10

返信ありがとうございます!先ほど、このようなroutes.rbファイルに『 post 'post/new' => 'post#new' 』の記述を加えました。 Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: "top#index" post 'post/new' => 'post#new' resources :post end このすることで、POSTメソッドを加えられたと思います。
suirunakamura

2020/08/09 08:17 編集

すると、次は先ほど新しく質問させていただいた ActionController::InvalidAuthenticityTokenエラーが表示されてしまいました。 https://i.gyazo.com/5e48e16ea33f88d1a10d91d1fa21c85d.png このエラー文の原因が本当にわからずにいます。 このエラー文の解決について何かご存知でしょうか?どうかご教示いただきたいです!
maisumakun

2020/08/11 10:06

> 先ほど、このようなroutes.rbファイルに『 post 'post/new' => 'post#new' 』の記述を加えました。 その時点で対処法が間違っています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問