現在、Ruby on Railsでコメントが出来るブログを開発しています。
作成されたコメントに対して返信が出来る機能を実装していますが、なかなか実装方法が思いつきませんので、アドバイスを頂きたいです。
現状のソースコードは以下の通りです。
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 :integer # # Indexes # # index_comments_on_deleted_at (deleted_at) # class Comment < ApplicationRecord ## # リレーション ## belongs_to :user belongs_to :post has_many :replies, class_name: 'Comment', foreign_key: :reply, dependent: :destroy end
comment_controller.rb # コメントの新規作成 def create @post = Post.find(params[:post_id]) @comment = @post.comments.new(comment_params) @comment.user_id = current_user.id if @comment.save redirect_to @post, notice: "コメントしました" else redirect_to @post, notice: "コメント出来ませんでした。" end end # コメントに返信 def reply # ここの実装がよく分かりません end
# コメントに返信するフォーム(Posts/show) - if @post.comments.present? - @post.comments.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 p = form.submit "上記の内容で返信する" # ここの実装も良く分かりません。。。
routes.rb resources :posts do resources :comments, only: [:create, :edit, :update, :destroy] do member do post :reply end end end
アドバイスなど、よろしくお願いいたします!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/01/23 01:31