12
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

railsのedit画面から確認(confirm)画面を挟んでupdateしようとしてエラーにハマった話

Posted at

どうもaono1234と申します。記事がいいなと思ったらtwitterのフォローもお待ちしております‼
https://twitter.com/takeshi_program

1. エラー状況

アプリ制作中にedit画面からconfirmアクションに遷移したらcouldn't find Feed with 'id'=confirmというエラーが発生してしまいました。😭
image.png

new画面からconfirmアクションに遷移するときはこのようなエラーは発生せず遷移できたのですが…🤔

まず、不可解な点はidの中に通常、番号が入るところが、cofirmという文字列が入ってしまっている点です。
このとっかかりから原因を突き止めることができました。

コードconfirmにパラメータ渡すときのlogを以下に載せておきますので、皆さんも考えてみて下さい。

2. コード

routes.rb
Rails.application.routes.draw do
  resources :feeds do
    collection do
      post :confirm
    end
  end
end
feeds_controller.rb
class FeedsController < ApplicationController
  before_action :set_feed, only: %i[ show edit update destroy ]

  def index
    @feeds = Feed.all
  end

  def show
  end

  def new
    if params[:back]
      @feed = Feed.new(feed_params)
    else
      @feed = Feed.new
    end
  end

  def edit
  end

  def create
    @feed = Feed.new(feed_params)
    respond_to do |format|
      if @feed.save
        format.html { redirect_to feed_url(@feed), notice: "Feed was successfully created." }
        format.json { render :show, status: :created, location: @feed }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @feed.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @feed.update(feed_params)
        format.html { redirect_to feed_url(@feed), notice: "Feed was successfully updated." }
        format.json { render :show, status: :ok, location: @feed }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @feed.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @feed.destroy
    respond_to do |format|
      format.html { redirect_to feeds_url, notice: "Feed was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  def confirm
    @feed = Feed.new(feed_params)
  end

  private
    def set_feed
      @feed = Feed.find(params[:id])
    end

    def feed_params
      params.require(:feed).permit(:image, :image_cache, :comment)
    end
end

3. log

newからconfirmに遷移するときのログ
Started POST "/feeds/confirm" for ::1 at 2022-10-08 00:29:55 +0900
Processing by FeedsController#confirm as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "feed"=>{"image_cache"=>"", "comment"=>"asdf"}, "commit"=>"登録する"}
  User Load (1.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   app/helpers/sessions_helper.rb:3:in `current_user'
Return value is: nil
  Rendering layout layouts/application.html.erb
  Rendering feeds/confirm.html.erb within layouts/application
  Rendered feeds/confirm.html.erb within layouts/application (Duration: 9.5ms | Allocations: 2360)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered layout layouts/application.html.erb (Duration: 25.2ms | Allocations: 6478)
Completed 200 OK in 30799ms (Views: 27.5ms | ActiveRecord: 1.6ms | Allocations: 94315)
editからconfirmに遷移するときのログ
Started PATCH "/feeds/confirm" for ::1 at 2022-10-08 00:49:57 +0900
Processing by FeedsController#update as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "feed"=>{"image_cache"=>"", "comment"=>"asdfaa"}, "commit"=>"更新する", "id"=>"confirm"}
  User Load (1.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   app/helpers/sessions_helper.rb:3:in `current_user'
  Feed Load (1.3ms)  SELECT "feeds".* FROM "feeds" WHERE "feeds"."id" = $1 LIMIT $2  [["id", nil], ["LIMIT", 1]]
  ↳ app/controllers/feeds_controller.rb:72:in `set_feed'
Completed 404 Not Found in 9ms (ActiveRecord: 2.6ms | Allocations: 1927)

4. 原因

エラーの理由はルートの設定にありました。
ログを見ると、newからconfirmに遷移するときはpostメソッドで遷移しているのに、
editからconfirmに遷移するときはpatchメソッドで遷移しています。

Started PATCH "/feeds/confirm" for ::1 at 2022-10-08 00:49:57 +0900

ルートにpatchメソッドを追加したらちゃんと遷移するようになりました!😄

routes.rb
Rails.application.routes.draw do
  resources :feeds do
    collection do
      post :confirm
      patch :confirm #追加
    end
  end
end
12
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?