@shino12656240 (篠原 寛人)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

rails 新規投稿について

解決したいこと

発生している問題・エラー

プログラミングを始めたばかりの者です。
Ruby on Railsで新規投稿画面を作成中です。
新規投稿画面はできたのですが投稿するとエラーになるので
解決方法を教えていただきたいです。
スクリーンショット 2023-01-14 19.24.08.png
スクリーンショット 2023-01-14 19.14.44.png
スクリーンショット 2023-01-14 19.15.54.png
スクリーンショット 2023-01-14 19.15.30.png





### 該当するソースコード
```言語名
``
ルート
Rails.application.routes.draw do
  root to: 'homes#top'
  get 'home/about' => 'homes#about'
  devise_for :users
 resources :post_clothe, only: [:new, :create, :index, :show] 
end

``

コントローラー

class PostClotheController < ApplicationController
  
  
def new
    @Post_clothe = PostClothe.new
end
     
def create
    @post_clothe = PostClothe.new (post_clothe_params)
    @post_clothe.user_id = current_user.id
    @post_clothe.save
    redirect_to post_clothe_path
end
    
  
  
  def index
    @Post_clothes = PostClothe.all
  end

  def show
  end
  
  private
  
  def post_clothe_params
    params.require(:post_clothe).permit(:title, :image, :explanation, :genre)
  end
end```
0 likes

1Answer

/post_clothe/newに対してPOSTしていますが該当するルーティングの定義がみつらないためエラーとなっています。
post_clotheはresourcesで定義しているようなので/post_clotheに対してPOSTするのが正しいです。
おそらくviewファイル(new.html.erb)のformで指定しているPOST先のURLを修正することで解決できると思います。

resourcesにより定義されるパスやコントローラ#アクションはこちらを参考にしてみてください。
https://railsguides.jp/routing.html#crud、verb、アクション

また、設定しているルーティングの定義はrails routesコマンドで確認できるので、同じようなエラーが起きた時に確認してみると良いですよ。

1Like

Your answer might help someone💌