Rails 5 - 实现可重用的评论模型 - 视图? [英] Rails 5 - implement reusable comment model - views?
问题描述
在我为学习 RoR 而构建的应用程序中,我遇到了类似的情况 问题.现在我的问题是如何改变我对此的看法?
In my app that I am building to learn RoR, I have a similar situation like this question. Now my question is how to change my views for this?
我有一个 Annotation 模型、一个 Document 模型和一个 Comment 模型.如果我切换到多态关联,以便我的 Annotations 和我的 Documents 可以有 Comments,如何进行视图(在部分中)?
I have an Annotation model, a Document model and a Comment model. If I switch to a polymorphic association such that my Annotations and my Documents can have Comments, how to do the view (in the partial)?
这是当前的部分:
<%= simple_form_for([@annotation, @annotation.comments.build], html: { class: 'form-vertical', multipart: true }) do |f| %>
<%= f.error_notification %>
<%= f.input :commenter, :as => :hidden, :input_html => { :value => current_user.username }, label: false %>
<%= f.input :body, placeholder: 'comment', focus: true, label: false %>
<%= f.button :submit, 'Save' %>
<% end -%>
更新
进一步研究并在我的注释视图中进行了如下更改:<%= render 'comments/form', :object =>@annotation%>
并在我的文档视图中如下: <%= render 'comments/form, :object =>@document %>
and as follows in my documents view: <%= render 'comments/form, :object => @document %>
并在我的部分进行了调整:
And adjusted this in my partial:
<%= simple_form_for([object, object.comments.build], html: { class: 'form-vertical', multipart: true }) do |f| %>
现在,当向文档添加评论时,我的 CommentsController 中出现错误 - 逻辑上是 :-) .
Now, when adding a comment to a document, I get an error in my CommentsController - logical that is :-) .
ActiveRecord::RecordNotFound in CommentsController#create
ActiveRecord::RecordNotFound in CommentsController#create
这是我当前的 CommentsController:
This is my current CommentsController:
class CommentsController < ApplicationController
def create
@annotation = Annotation.find(params[:annotation_id])
@comment = @annotation.comments.create(comment_params)
redirect_to annotation_path(@annotation)
end
def destroy
@annotation = Annotation.find(params[:annotation_id])
@comment = @annotation.comments.find(params[:id])
@comment.destroy
redirect_to annotation_path(@annotation)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
如何(最好)改变这一点?
How (best) to change this ?
我现在已经使用 :as => 为另一个名为tag"的对象/模型/类实现了相同的功能.:tagable
并且我可以为注释创建、列出等,但我无法删除.
I now have implemented the same for another object/model/class called "tag" using :as => :tagable
and I can create, list, etc for the Annotation, yet I cannot delete.
列表(作为部分)被调用:
The listing (as a partial) is called with:
<%= render 'tags/tag_list', :object => @annotation %>
或:
<%= render 'tags/tag_list', :object => @document %>
打开注释/文档记录时,抛出错误:
When opening the Annotation / Document record, it throws the error:
未定义的方法`object' #你的意思?object_id
undefined method `object' for # Did you mean? object_id
...在这一行:
<td><%= link_to '', [tag.object, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-remove" %></td>
我应该改变什么??
换行
<td><%= link_to '', [object, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-remove" %></td>
推荐答案
你必须以某种方式获取正确的可评论.最简单的解决方案是这样的:
You have to fetch correct commentable somehow. The simplest solution would be like this:
def create
commentable = detect_commentable
commentable.comments.create(comment_params)
redirect_to commentable_path(commentable)
end
private
def commentable_path(commentable)
case commentable
when Document
document_path(commentable)
when Annotation
annotation_path(commentable)
else
fail 'unknown commentable'
end
end
def detect_commentable
if params[:annotation_id]
Annotation.find(params[:annotation_id])
elsif params[:document_id]
Document.find(params[:document_id])
else
fail 'Commentable not found'
end
end
这显然不是最好的代码(一方面是因为维护需求).但这应该会让你开始.
It is not the best code, obviously (because of maintenance requirements, for one thing). But this should get you started.
这篇关于Rails 5 - 实现可重用的评论模型 - 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!