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

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

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

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

Ruby

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

Ruby on Rails

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

ルーティング

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

Q&A

解決済

2回答

2480閲覧

No route matches [POST] エラー

adonson

総合スコア12

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

ルーティング

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

0グッド

0クリップ

投稿2020/04/07 08:55

No route matches [POST]エラーについて

現在PCの構成部品を選択し、そのPCの名前と構成部品をPCテーブルに登録するアプリを作成しています。
登録画面(app/view/pc/new.html.haml)の作成は完了しましたが、submitボタンを押すと「No route matches [POST] "/pc/new"」のルーティングエラーが発生します。
rails routesでルーティングを確認したところPOSTはあるので、なぜこのエラーが出るかわかりません。
どのように修正すればよろしいでしょうか。
よろしくお願いします。

app/view/pc/new.html.haml .new .content = form_with model: @pcs, local:true, class:"content_wrapper" do |f| .pc .name = f.text_field :name, class:"pc_name", placeholder: "PC名", maxlength: "20",required: true .cpu = f.collection_select :cpu_id, Cpu.all.order(name: :asc), :id, :name .mother_board = f.collection_select :mother_board_id, MotherBoard.all.order(name: :asc), :id, :name .memory = f.collection_select :memory_id, Memory.all.order(name: :asc), :id, :name .videocard = f.collection_select :videocard_id, Videocard.all.order(name: :asc), :id, :name .pc_case = f.collection_select :pc_case_id, PcCase.all.order(name: :asc), :id, :view_name_and_color .power_unit = f.collection_select :power_unit_id, PowerUnit.all.order(name: :asc), :id, :name .ssd = f.collection_select :ssd_id, Ssd.all.order(name: :asc), :id, :name .cpu_cooler = f.collection_select :cpu_cooler_id, CpuCooler.all.order(name: :asc), :id, :name .private %input{name: "private", type: "checkbox", value: "1"} %label{for: "private"} 公開する .submit = f.submit :name, class:"content__wrapper__confirmation__button", value: "確認"
rails routes Prefix Verb URI Pattern pc_index GET /pc(.:format) pc#index POST /pc(.:format) pc#create new_pc GET /pc/new(.:format) pc#new edit_pc GET /pc/:id/edit(.:format) pc#edit pc GET /pc/:id(.:format) pc#show PATCH /pc/:id(.:format) pc#update PUT /pc/:id(.:format) pc#update DELETE /pc/:id(.:format) pc#destroy cpu_index GET /cpu(.:format) cpu#index cpu GET /cpu/:id(.:format) cpu#show mother_board_index GET /mother_board(.:format) mother_board#index mother_board GET /mother_board/:id(.:format) mother_board#show memory_index GET /memory(.:format) memory#index memory GET /memory/:id(.:format) memory#show videocard_index GET /videocard(.:format) videocard#index videocard GET /videocard/:id(.:format) videocard#show pc_case_index GET /pc_case(.:format) pc_case#index pc_case GET /pc_case/:id(.:format) pc_case#show power_unit_index GET /power_unit(.:format) power_unit#index power_unit GET /power_unit/:id(.:format) power_unit#show ssd_index GET /ssd(.:format) ssd#index ssd GET /ssd/:id(.:format) ssd#show cpu_cooler_index GET /cpu_cooler(.:format) cpu_cooler#index cpu_cooler GET /cpu_cooler/:id(.:format) cpu_cooler#show root GET / top#index 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
app/controller/pc_controller.rb class PcController < ApplicationController before_action :authenticate_user!, except: [:index,:show] def index @pc = Pc.all end def show @pc = Pc.find(params[:id]) end def new @pc = Pc.new end def create @pc = Pc.new(pc_params) respond_to do |format| if @pc.save format.html { redirect_to @pc, notice: '作成成功' } format.json { render :show, status: :created, location: @pc } else format.html { render :new } end end end def edit @pc = Pc.find(params[:id]) if current_user.id == @pc.user.id else redirect_to pc_path(@pc) end end def update @pc = Pc.find(params[:id]) if @pc.update(pc_params) redirect_to pc_path(@pc) else render :edit end end def destroy pc = Pc.find(params[:id]) unless pc.destroy render :show end end end
config/routes.rb Rails.application.routes.draw do root 'top#index' devise_for :users, :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions' } devise_scope :user do get "user/:id", :to => "users/registrations#detail" get "signup", :to => "users/registrations#new" get "login", :to => "users/sessions#new" get "logout", :to => "users/sessions#destroy" end resources :pc, only: [:index, :create, :new, :show, :edit, :update, :destroy] resources :cpu, only: [:index, :show] resources :mother_board, only: [:index, :show] resources :memory, only: [:index, :show] resources :videocard, only: [:index, :show] resources :pc_case, only: [:index, :show] resources :power_unit, only: [:index, :show] resources :ssd, only: [:index, :show] resources :cpu_cooler, only: [:index, :show]

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

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

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

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

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

guest

回答2

0

ベストアンサー

載っているcodeがエラーが起きた時の物と異なるのではないか?と感じてます。
理由
0. routes.rb からみて indexへのPOSTが用意されるのが不思議
0. index.htmlで @pcs が def index で未定義のまま使われている。
0. そのform_with で index へのrouteができるのが不思議

indexと思われるviewは new や edit の内容に見えます。
一覧画面で新規登録もできると言うのもありですが、一覧なしで新規登録させるのでしたら、newにしたほうが読む人の誤解や混乱も減るのでそちらをお薦め。

投稿2020/04/08 21:21

編集2020/04/08 21:26
winterboum

総合スコア23329

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

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

0

new_pc GET /pc/new(.:format)

GETしかないように見えますが?

投稿2020/04/07 09:36

編集2020/04/07 09:37
Takumiboo

総合スコア2534

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

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

winterboum

2020/04/08 21:21

pc_index GET /pc(.:format) の下に POST /pc(.:format) があるので、POSTも有りますね。 この routes.rbでなぜ inex に POST ができるのか???ではありますが
adonson

2020/04/08 23:53

ご回答ありがとうございます。 routes.rbで resources :pc, only: [:index, :create, :new, :show, :edit, :update, :destroy] のcreateの記載でnewにPOSTが付与されるという解釈だったのですが、現在はINDEXに付与されているので、newにPOSTがつくようにするにはを調べて見ます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問