对多种模型的评论 [英] Comments on multiple models
问题描述
在我的rails应用程序中,我目前具有注释设置,可用于我的帖子模型,该模型运行正常。如何在书本模型中添加注释?
Within my rails app, I currently have comments setup to work with my posts model, which is functioning properly. How do I add comments to my books model?
这是我到目前为止的内容:
Here is what I have so far:
这里是我在注释中具有的模式如下:
Here is what I have in my schema for the comments:
create_table "comments", force: true do |t|
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "post_id"
t.integer "book_id"
end
在我的用户模型中:
class User < ActiveRecord::Base
has_many :comments
acts_as_voter
end
在我的帖子模型中:
class Post < ActiveRecord::Base
has_many :comments
end
在我的书本模型中:
class Book < ActiveRecord::Base
has_many :comments
end
在我的评论模型中:
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :book
belongs_to :user
acts_as_votable
end
在我的评论控制器中:
class CommentsController < ApplicationController
def create
post.comments.create(new_comment_params) do |comment|
comment.user = current_user
end
respond_to do |format|
format.html {redirect_to post_path(post)}
end
end
def upvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.liked_by current_user
respond_to do |format|
format.html {redirect_to @post}
end
end
private
def new_comment_params
params.require(:comment).permit(:body)
end
def post
@post = Post.find(params[:post_id])
end
end
在我的路线文件中:
resources :posts do
resources :comments do
member do
put "like", to: "comments#upvote"
end
end
end
在我看来:
<% @post.comments.each do |comment| %>
<%= comment.body %>
<% if user_signed_in? && (current_user != comment.user) && !(current_user.voted_for? comment) %>
<%= link_to "up vote", like_post_comment_path(@post, comment), method: :put %>
<%= comment.votes.size %>
<% else %>
<%= comment.votes.size %></a>
<% end %>
<% end %>
<br />
<%= form_for([@post, @post.comments.build]) do |f| %>
<p><%= f.text_area :body, :cols => "80", :rows => "10" %></p>
<p><%= f.submit "comment" %></p>
<% end %>
我如何在评论控制器中添加内容,以使评论可以同时在帖子和书籍上使用?我要在路由文件中添加什么?
What do I add to my comments controller to get comments working on both posts and books? What do I add to my routes file?
在此先感谢您的帮助。
推荐答案
您不想指定每种可以容纳 Comment
对象的对象。到处都是 if-elsif-else
块的头痛。相反,您希望事物是 Commentable
,并且它们都将带有 .comments
。
You don't want to specify each type of object that can hold Comment
objects. That creates a headache of if-elsif-else
blocks all over the place. Instead, you want things to be Commentable
, and they all will have .comments
on them.
在此称为多态关联活动记录。因此,您将得到如下模型:
This is called a polymorphic association in Active Record. So you would have your models something like:
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
class Post < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Book < ActiveRecord::Base
has_many :comments, as: :commentable
end
并相应地修改数据库,这些都在链接的文章中。现在,当您为表单构建 Comment
对象时,该对象将预先填充 commentable_id
和 commentable_type
,您可以在隐藏字段中使用它。现在,与 Comment
关联的内容无关紧要,您始终将其视为相同。
And modify your database accordingly, it's all in the linked article. Now when you build a Comment
object for a form, it will have pre-populated a commentable_id
and commentable_type
, which you can toss in hidden fields. Now it doesn't matter what the Comment
is associated with, you always treat it the same.
我d将 User
留为一个单独的关联,因为这实际上不是一个相同的想法。
I'd leave User
as a separate association, since it's not really the same idea.
这篇关于对多种模型的评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!