コメント付きブログを開発しております。Railsのform_forについて質問があります。
コメントに返信をする機能を実装しておりますが、返信のフォームがなかなか上手く実装できません。
色々と試しましたが、解決できなかったので、教えて頂けますと幸いです。
【問題点】
返信用のtext_areaに親のコメントのcontent(内容)がデフォルトで入ってしまう点。
デフォルトで入ったcontent(内容)を削除して、入力したら正しく返信できるが、最初から返信用のtext_areaを空の状態にしたい。
Comment.rb # id :integer not null, primary key # user_id :integer not null # post_id :integer not null # content :text # created_at :datetime not null # updated_at :datetime not null # deleted_at :datetime # reply_id :integer belongs_to :user belongs_to :post # 返信 has_many :replies, class_name: 'Comment', foreign_key: :reply_id, dependent: :destroy # reply_idがnilなら親コメント scope :parent_comment, -> { where(reply_id: nil) }
comment_controller.rb # 返信用のメソッド def reply @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @reply = @comment.replies.new(comment_params) @reply.user_id = current_user.id @reply.post = @post if @reply.save redirect_to @post, notice: "返信しました" else redirect_to @post, notice: "返信出来ませんでした。" end end
posts/show.html.slim - if @post.comments.present? - @post.comments.parent_comment.each do |comment| p.commenter_name = comment.user.try(:name) p.comment.content = comment.content - comment.replies.each do |reply| p.comment.content = reply.content = form_for [:reply, @post, comment], method: :post do |form| p = form.text_area :content, rows: 3, cols: 35 # ↑ここのtext_areaに最初から親コメントのcontent(内容)が入った状態になってしまいます p = form.submit "上記の内容で返信する"
色々と試してみましたが、上手くいきませんでした。どなたか教えていただけないでしょうか?宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/01/26 12:36 編集
2018/01/26 13:17
退会済みユーザー
2018/01/26 14:49 編集
退会済みユーザー
2018/01/26 22:27 編集
2018/01/28 04:01
退会済みユーザー
2018/01/28 11:07 編集