railsの投稿アプリでコメント機能を実装したいのですが、上手くできずに下のようなエラーになってしまいます。
どんなことでもいいので、間違っている所や解決策を教えて頂きたいです。よろしくお願いします。
諸事情によりcommentではなくreplyという名前でモデルを作ってしまったため、replyはcommentと認識していただいて大丈夫です。
/config/routes.rb↓
Rails.application.routes.draw do devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html get 'tops/top' => 'tops#top' get 'tops/mypage' => 'tops#mypage' root 'tops#top' resources :posts do resources :replies, only: [:create, :destroy] end end
/posts_controller.rb↓
class PostsController < ApplicationController before_action :authenticate_user! def index if params[:search] #部分検索 @posts = Post.where("content LIKE ? ",'%' + params[:search] + '%').page(params[:page]).per(5) else @posts= Post.all.page(params[:page]).per(5) end end def new @post = Post.new end def create @post = Post.new(post_params) @post.user_id = current_user.id #新しいTweetの保存に成功した場合 if @post.save #index.html.erbにページが移る redirect_to action: "index" #新しいTweetの保存に失敗した場合 else #もう一回投稿画面へ redirect_to action: "new" end end def show @post = Post.find(params[:id]) @replies = @post.replies @reply = Reply.new end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to :action => "show", :id => @post.id else redirect_to :action => "new" end end def destroy Post.find(params[:id]).destroy redirect_to action: :index end private #セキュリティのため、許可した:bodyというデータだけ取ってくるようにする def post_params params.require(:post).permit(:content) end end
/replies_controller.rb↓
class RepliesController < ApplicationController def create @reply = Reply.new(content_params) if @reply.save flash[:success] = "コメントしました" redirect_to controller: posts:, action: :show else flash[:danger] = "コメントできません" redirect_to controller: posts:, action: :show end end def destroy @reply = Reply.find(params[:id]) @reply.destroy redirect_to controller: posts:, action: :show end private def content_params params.require(:reply).permit(:content) end end
/views/posts/show.html.erb↓
h1>SUSAKU</h1> <h3>質問詳細</h3> <div class=post"> <p><%= @post.content %></p> <p><%= @post.created_at %></p> <%= link_to "編集する", edit_post_path(@post.id) %> <%= link_to "削除する", post_path(@post.id), method: :delete %> </div> <h2>コメント一覧</h2> <% @replies.each do |r| %> <div> <%= r.id %> <%= r.content %> <%= r.created_at %> <hr> </div> <% end %> <%= form_for [@post, @reply] do |f| %> <%= f.text_field :content %> <br> <%= f.submit 'コメントする' %> <% end %> <%= link_to "質問一覧に戻る", posts_path %>
/models/post.rb↓
class Post < ApplicationRecord has_many :replies, dependent: :destroy belongs_to :user end
/models/reply.rb↓
class Reply < ApplicationRecord belongs_to :post validates :content, presence: true end
/db/migrate/20191227071624_create_posts.rb↓
class CreatePosts < ActiveRecord::Migration[6.0] def change create_table :posts do |t| t.string :image t.string :image2 t.string :image3 t.string :image4 t.string :title t.text :content t.boolean :done t.integer :user_id t.timestamps end end end
/db/migrate/20200106125455_create_replies.rb
class CreateReplies < ActiveRecord::Migration[6.0] def change create_table :replies do |t| t.text :content t.references :post, null: false, foreign_key: true t.timestamps end end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/08 09:36 編集
2020/01/08 10:19 編集
2020/01/08 10:23