前提・実現したいこと
現在、レシピ投稿サイトを作成しているのですが、レシピの詳細ページ(show)で
レシピが投稿されていない場合にメッセージを表示させたいと考えています。
発生している問題・エラーメッセージ
対応するidがないと表示されています。 実際にidはないためエラーが出ています。 https://gyazo.com/aa0f142f5bd9d7adc012fd18f91d84ab
該当のソースコード(show)
ruby
1%h1 2 ="#{@user.nickname}さんのレシピ" 3-if @recipe.present? 4 .recipe_title-show 5 =@recipe.title 6-else 7 投稿されているレシピはありません
rails
1class RecipesController < ApplicationController 2 def index 3 @recipes = Recipe.all.order(created_at:"DESC") 4 end 5 6 def new 7 @recipes = Recipe.new 8 end 9 10 def create 11 @recipes = current_user.recipes.build(recipe_params) 12 if @recipes.save 13 flash[:register] = "投稿できました!" 14 redirect_to root_path 15 else 16 flash[:false] = "レシピの投稿に失敗しました" 17 redirect_to root_path 18 end 19 end 20 21 def show 22 @recipe = Recipe.find(params[:id]) 23 @user = User.find_by(id: @recipe.user_id) 24 end 25 26 private 27 def recipe_params 28 params.require(:recipe).permit(:title, :content, :recipe_image) 29 end 30end 31 32 33
試したこと
showページで、@recipe.present?や@recipe.title.present?などに書き換えてみましたが、
解決しませんでした。
初心者で、質問自体が基礎的な事だと思いますが、何卒ご教授の程宜しくお願い致します。
また、不足している情報がありましたら併せて教えて頂けると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。