railsで写真のように画像投稿詳細ページ(Post_images)に付随するようにコメント機能(Post_comments)を付けたのですが、post_comments_controller.rb上のコードのcreateアクションの部分がいまいち理解できていないので、そこの部分のコードの詳しい説明をしていただきたいです。また、おそらく変数とメソッドが理解できていない故だと思うので、併せて説明していただきたいです。よろしくお願い致します。
rails
1class PostCommentsController < ApplicationController 2 def create 3 post_image = PostImage.find(params[:post_image_id]) 4 comment = current_user.post_comments.new(post_comment_params) 5 comment.post_image_id = post_image.id 6 comment.save 7 redirect_to post_image_path(post_image) 8 end 9 # データを保存する処理を行う 10 # newアクションはあくまで投稿フォーム画面を表示するだけで、投稿フォーム自体はform_forでOK 11 12 13 private 14 def post_comment_params 15 params.require(:post_comment).permit(:user_id,:post_image_id, :comment) 16end 17end 18
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/27 02:26
2019/03/27 02:36
2019/03/27 08:09