0
0

More than 3 years have passed since last update.

Couldn't find Blog with 'id'=show

Posted at

エラーについて

paramの値が空か間違っているという意味です。
今回の場合は、投稿→showに遷移する際にこのエラーを検出しました。

エラーの原因

pathにid指定されていなかったことが今回の原因でした。

controller.rb
def create
    @book = Book.new(book_params)
    if @book.save
      flash[:notice] = "Book was successfully created."
      redirect_to "/books/show"
    else
      @books = Book.all
      render :index
    end
end

if文の中のredirect_toにidが指定されていないことが原因あると分かりました。

解決方法

redirect_toに関するドキュメントを参考にしながら以下のように書き替えました。 

controller.rb
 def create
    @book = Book.new(book_params)
    @book.user_id = current_user.id
    if @book.save
      redirect_to book_path(@book), notice: "You have created book successfully."
    else
      @books = Book.all
      render 'index'
    end
 end

そうすることによって、投稿の詳細ページにidが渡され、エラーが解決しました。

まとめ

redirect_toの書き方が間違っていた。
このエラーが出て確認すべきポイントは、idという引数がしっかり送られているかについて考えると解決策が見えてくると思います。
何かご意見等あれば、ご指摘よろしくお願い致します。

0
0
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
0
0