Ruby on rails 对多种模式的评论
在我的rails应用程序中,我目前有评论设置来处理我的帖子模型,该模型运行正常。如何向我的图书模型添加注释 以下是我到目前为止的情况: 以下是我的注释模式中的内容:Ruby on rails 对多种模式的评论,ruby-on-rails,ruby,ruby-on-rails-4,crud,Ruby On Rails,Ruby,Ruby On Rails 4,Crud,在我的rails应用程序中,我目前有评论设置来处理我的帖子模型,该模型运行正常。如何向我的图书模型添加注释 以下是我到目前为止的情况: 以下是我的注释模式中的内容: create_table "comments", force: true do |t| t.text "body" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" t.integer "pos
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
我认为:
<% @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 %>
“80”,:行=>“10”%>
我应该向我的评论控制器添加什么才能让评论在文章和书籍上都起作用?我应该向路由文件添加什么
提前感谢您提供的帮助。您不想指定每种类型的对象,这些对象可以容纳
注释
对象。这造成了一个头痛的问题,那就是到处都是if-elsif-else
块。相反,您希望事物是可注释的,并且它们都会有.comments
这称为活动记录中的。所以你的模型应该是这样的:
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
class注释
并相应地修改您的数据库,这些都在链接文章中。现在,当您为表单构建一个Comment
对象时,它将预先填充一个commentable\u id
和commentable\u type
,您可以将其放入隐藏字段中。现在,不管注释与什么相关,您总是以相同的方式对待它
我将把
用户
作为一个单独的协会,因为这不是一个真正相同的想法。试着按照这个简单的例子:谢谢你的帮助,而不是像这里的一些人那样是一个自命不凡的人!对于我的注释表,我是否应该删除post\u id和book\u id并添加可注释的\u id:integer可注释的\u type:string?我还可以继续使用\u id吗?我可以使用本教程中的控制器代码吗?对于路线,我可以使用本教程中的代码吗?如何将投票添加到routes中的注释中?控制器代码在那篇文章中看起来不错。至于路线,看起来有点过时了。您可以使用与以前相同的资源
路由,但您可能希望将它们指向一个公共控制器,具体取决于您所做的工作。本教程涵盖了许多选项。至于向上投票,使用upvote
控制器方法并使用acts\u As\u votable
。运行rake routes
查看可用的路由,您似乎走上了正确的轨道。您可以尝试多态路径
,但不确定这是否会根据您的情况有所帮助。还有关于多态性关联和路由。像这样更具体的问题作为一个全新的问题可能会更好。
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 %>
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