ruby on rails コメント投稿実装 エラー
コメント機能を実装したところエラーが出ました。
どこを変えればいいのか分からないので教えて頂きたいです。
よろしくお願い致します。
参考にしたサイトはこちらです。
https://qiita.com/__kotaro_/items/8a6bda99dab61d2a72a5
model/posts.rb
class Post < ApplicationRecord
validates :content, {presence: true, length: {maximum: 140}}
validates :user_id, {presence: true}
has_many :comments
def user
return User.find_by(id: self.user_id)
end
end
model/user.rb
class User < ApplicationRecord
has_secure_password
validates :name, {presence: true}
validates :email, {presence: true, uniqueness: true}
has_many :comments
def posts
return Post.where(user_id: self.id)
end
end
model/comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :post, optional: true
end
コントローラーcomments_controller.rb
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
post = Post.find(params[:post_id])
@comment = post.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "コメントしました"
redirect_back(fallback_location: root_path)
else
flash[:success] = "コメントできませんでした"
redirect_back(fallback_location: root_path)
end
end
private
def comment_params
params.require(:comment).permit(:content)
end
end
posts_controller.rb
class PostsController < ApplicationController
before_action :authenticate_user
before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
def index
@posts = Post.all.order(created_at: :desc)
end
def show
@post = Post.find_by(id: params[:id])
@user = @post.users ←ここでエラーが出ます。
@post = Post.find(params[:id])
@comments = @post.comments
@comment = @post.comments.build
end
def new
@post = Post.new
end
def create
@post = Post.new(
content: params[:content],
user_id: @current_user.id
)
if @post.save
flash[:notice] = "投稿を作成しました"
redirect_to("/posts/index")
else
render("posts/new")
end
end
def edit
@post = Post.find_by(id: params[:id])
end
def update
@post = Post.find_by(id: params[:id])
@post.content = params[:content]
if @post.save
flash[:notice] = "投稿を編集しました"
redirect_to("/posts/index")
else
render("posts/edit")
end
end
def destroy
@post = Post.find_by(id: params[:id])
@post.destroy
flash[:notice] = "投稿を削除しました"
redirect_to("/posts/index")
end
def ensure_correct_user
@post = Post.find_by(id: params[:id])
if @post.user_id != @current_user.id
flash[:notice] = "権限がありません"
redirect_to("/posts/index")
end
end
end
routes.rb
Rails.application.routes.draw do
get "login" => "users#login_form"
post "login" => "users#login"
post "logout" => "users#logout"
post "users/:id/update" => "users#update"
get "users/:id/edit" => "users#edit"
post "users/create" => "users#create"
get "singnup" => "users#new"
get "users/index" => "users#index"
get "users/:id" => "users#show"
get "posts/index" => "posts#index"
get "posts/new" => "posts#new"
get "posts/:id" => "posts#show"
resources :posts do
resources :comments, only: [:create]
end
post "posts/create" => "posts#create"
get "posts/:id/edit" => "posts#edit"
post "posts/:id/update" => "posts#update"
post "posts/:id/destroy" => "posts#destroy"
get "/" => "home#top"
get "about" => "home#about"
end
viwes/posts.show.html.erb
<div class="main posts-show">
<div class="container">
<div class="posts-show-item">
<div class="post-user-name">
<img src="<%= "/user_images/#{@user.image_name}" %>">
<%= link_to(@user.name, "/users/#{@user.id}") %>
</div>
<p>
<%= @post.content %>
</p>
<div class="post-time">
<%= @post.created_at %>
</div>
<% if @post.user_id == @current_user.id %>
<div class="post-menus">
<%= link_to("編集", "/posts/#{@post.id}/edit") %>
<%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "post"}) %>
</div>
<% end %>
</div>
</div>
</div>
#これから下がcommentコードです。
<div class="comment-wrapper border-top mb-10">
<p class="mt-5">コメント一覧</p>
<% @comments.each do |c| %>
<div>
<% unless c.user.blank? %>
<a href="<%= user_path(c.user_id) %>"><%= image_tag c.user.image.to_s,
class:"rounded-circle icon_image mr-3 mb-3"%></a>
<% end %>
<%= c.user.username unless c.user.blank? %>
<br />
<%= c.content %>
</div>
<br />
<% end %>
<% if user_signed_in? %>
<%= form_with(model: [@post, @comment], local: true) do |f| %>
<%= f.text_area :content, class: "form-control", rows: 5 %>
<%= button_tag type: "submit", class: "btn btn-success float-right mt-1" do %>
<i class="far fa-comments"></i> コメントする
<% end %>
<% end %>
<% end %>
</div>
0