所以我正在尝试为我的帖子在rails项目中构建一个评论功能,但是想要查看视图和控制器以便能够成功地对帖子发表评论。我的方式是我的新闻Feed中的每个帖子都有一个链接,名为评论,将您重定向到显示单个帖子的节目页面。在那个页面上我希望有一个表格,就像我在主页上的帖子表格一样,但是这个表格可以用来评论帖子,任何帮助找出控制器,表单以便评论,并让它们显示在帖子下面。发布展示页面将非常感谢
这是Post控制器的控制器和视图模板
class PostsController < ApplicationController
before_action :logged_in_user, only: [:show, :create, :destroy]
before_action :correct_user, only: :destroy
def show
@post = Post.find(params[:id])
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
redirect_to request.referrer || root_url
end
private
def post_params
params.require(:post).permit(:content, :picture)
end
def correct_user
@post = current_user.posts.find_by(id: params[:id])
redirect_to root_url if @post.nil?
end
end
帖子显示页面
<% if logged_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
</aside>
<div class="col-md-8 posts">
<h3>Comments</h3>
<%= render 'posts/post_show' %>
</div>
</div>
<% end %>
我的评论控制器到目前为止
class CommentsController < ApplicationController
before_action :loged_in_user, only:[:create, :destroy]
before_action :correct_user, only: :destroy
def create
@post = Post.find(params[:id])
@comment = Comment.new(params[:comment])
@comment.post = @post
@comment.user = current_user
if @comment.save
redirect_to post
else
render 'post/show'
end
end
def destroy
end
private
def comment_params
params.require(:comment).permit(:content, :picture)
end
def correct_user
@comment = current_user.comment.find_by(id: params[:id])
render 'post/show' if @comment.nil?
end
end
我的帖子部分呈现方式如何在帖子显示页面的帖子下以相同的方式呈现评论
<li id="post-<%= post.id %>">
<%= link_to gravatar_for(post.user), post.user %>
<span class="user"><%= link_to post.user.name, post.user %></span>
<span class="content"><%= post.content %></span>
<%= image_tag post.picture.url if post.picture? %>
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago.
<%= link_to "comments", post %>
<% if current_user?(post.user) %>
<%= link_to "delete", post, method: :delete, data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
我的帖子模型
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 500 }
validate :picture_size
private
def picture_size
if picture.size > 10.megabytes
errors.add(:picture, "should be less than 10MB")
end
end
end
和评论模型
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
default_scope -> { order(created_at )}
validates :user_id , pressence: true
validates :post_id , pressence: true
validates :content, presence: true, length: { maximum: 500 }
validate :picture_size
private
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
这是我的用户模型
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 250 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
def forget
update_attribute(:remember_digest, nil)
end
def activate
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
def feed
Post.where("user_id = ?", id)
end
private
def downcase_email
self.email = email.downcase
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
路由文件
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:show, :create, :destroy]
resources :comments, only: [:show, :create, :destroy]
end
答案 0 :(得分:0)
您可以从路径文件开始。首先,您需要为属于帖子的评论定义路线。
以下是:
resources :posts do
resources :comments
end
这样,您就可以编辑属于/posts/3/comments/2
等特定帖子的评论。此路径会将您带到id
2的评论,该评论属于id
为3的帖子。
您还需要在CommentsController
中重新定义所有方法。例如,
def update
@post = Post.find(params[:id])
@comment = Comment.find(params[:comment_id])
...
end
嗯,这只是你应该做的事情的潜行高峰。在这里显示所有程序确实非常冗长。查看this以进一步了解它是如何适合的。