, , README - . @Kunday. act_as_commentable ...
, "", . gem, rails g comment, .
.
-, , , , gem README.
class Post < ActiveRecord::Base
acts_as_commentable
end
create. , :authenticate_user! , . current_user . , / .
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
post = Post.find_by(id: params[:id])
comment = post.comments.build(comment_params)
comment.user = current_user
if comment.save
flash[:notice] = "Comment has been created."
redirect_to post
else
flash[:alert] = "Comment has not been created."
end
end
private
def comment_params
params.permit(:comment)
end
end
. . , - , .
post 'comments' => 'comments#create', as: "create_comment"
as: "create_comment" , create_comment_path. , Post, . css.
<div class="comment-section">
<%= form_tag create_comment_path, method: "post" do %>
<%= hidden_field_tag "id", @post.id %>
<%= text_area_tag :comment %>
<%= submit_tag "Submit" %>
<% end %>
</div>
, Post show.
css, comment.user.name , . email , .
<div class="comment_list">
<% @comments.each do |comment| %>
<%= comment.comment %> <br>
<%= comment.user.name %> <br>
<br>
<% end %>
</div>
, , , @comments , Post , :
def show
@post = Post.find_by(id: params[:id])
@comments = @post.comments.all
end
. .