質問編集履歴

2

追記しました

2023/01/30 02:24

投稿

yyhoshino
yyhoshino

スコア1

test CHANGED
File without changes
test CHANGED
@@ -60,6 +60,78 @@
60
60
  $("input").val("");
61
61
  $('.recipe-comments').html("<%= escape_javascript(render 'comments', comment: @comment) %>")
62
62
  ```
63
+ recipes_controllerの追記
64
+ ```ruby.recipes_controller
65
+ class RecipesController < ApplicationController
66
+ before_action :authenticate_user!, except: [:index, :show]
67
+ before_action :set_recipe, only: [:show, :destroy, :edit, :update,]
68
+ before_action :contributor_confirmation, only: [:edit, :update, :destroy]
69
+ def index
70
+ @recipes = Recipe.order(id: 'DESC')
71
+ end
72
+
73
+ def new
74
+ @recipe = Recipe.new
75
+ end
76
+
77
+ def show
78
+ @comment = Comment.new #新規コメント用
79
+ @comments = @recipe.comments.includes(:user) #コメント表示用投稿に関連づくコメントの取得
80
+ end
81
+
82
+ def edit
83
+ end
84
+
85
+ def update
86
+ if @recipe.update(recipe_params)
87
+ return redirect_to recipe_path(@recipe)
88
+ else
89
+ render 'edit'
90
+ end
91
+ end
92
+
93
+ def destroy
94
+ @recipe.destroy
95
+ redirect_to root_path
96
+ end
97
+
98
+ def create
99
+ @recipe = Recipe.create(recipe_params)
100
+ if @recipe.save
101
+ redirect_to root_path
102
+ else
103
+ render 'new'
104
+ end
105
+ end
106
+
107
+ def search
108
+ @q = Recipe.ransack(params[:q])
109
+ @recipes = @q.result
110
+ end
111
+
112
+ private
113
+ def recipe_params
114
+ params.require(:recipe).permit(
115
+ :dish_name,
116
+ :ingredient,
117
+ :make,
118
+ :introduction,
119
+ :image,
120
+ :moon_age_id,
121
+ :classification_id)
122
+ .merge(user_id: current_user.id
123
+ )
124
+ end
125
+
126
+ def set_recipe
127
+ @recipe = Recipe.find(params[:id])
128
+ end
129
+
130
+ def contributor_confirmation
131
+ redirect_to root_path unless current_user == @recipe.user
132
+ end
133
+ end
134
+ ```
63
135
 
64
136
 
65
137
  ### 試したこと

1

誤字

2023/01/26 05:36

投稿

yyhoshino
yyhoshino

スコア1

test CHANGED
File without changes
test CHANGED
@@ -64,7 +64,7 @@
64
64
 
65
65
  ### 試したこと
66
66
 
67
- create.js.erbファイルの記述に自信がなく、1〜2業目でテキストエリアを空にして、3行目でshow.html.erbの”recipe-commentsクラス”を呼び出し、投稿したコメントを呼び出しているので、動と思ったのですがうまくいきませんでした。
67
+ create.js.erbファイルの記述に自信がなく、1〜2業目でテキストエリアを空にして、3行目でshow.html.erbの”recipe-commentsクラス”を呼び出し、投稿したコメントを呼び出しているので、動作すると思ったのですがうまくいきませんでした。
68
68
 
69
69
  ### 補足情報(FW/ツールのバージョンなど)
70
70