1

本质上,我有一个喜欢/不喜欢的二元投票系统。你的类被称为Like它与likeable具有多态关联:

class Like < ActiveRecord::Base
     belongs_to :likeable, polymorphic: true
end

我们有这个类Comment,它也有可评论的多态关联,并且可以被喜欢

class Comment < ActiveRecord::Base
     belongs_to :commentable, polymorphic: true
     has_many :likes, :as :likeable
end

我们有课Section,也可以点赞和评论

class Section < ActiveRecord::Base
    has_many :likes, as: :likeable
    has_many :comments, as: commentable
end

但是,在页面上,section#show我显示部分信息,部分喜欢,然后是评论(来自comments/comments部分)。这是Section#show视图:

<h1><%= exercise.name %></h1>
<p><%= exercise.description %></p>
<%= render 'likes/like_button' %>
<%= render 'comments/comments' %>
<%= render 'comments/comment_form' %>

但是,我希望能够对每条评论进行投票。

以下代码来自_comments.html.erb- 当前不起作用的是渲染,_like_button.html.erb因为它不适用于手头的评论。

<% @comments.each do |comment| %>
    <%= comment.content %>
    <%= render 'likes/like_button' %>
<hr />
<% end %>

这是_like_button.html.erb部分

<% if @like.nil? %>
    <%# No record of Like in table %>
    <%= form_for [@likeable, Like.new] do |f| %>
    <%= f.submit "Like" %>
    <%= f.submit "Dislike" %>
    <% end %>
<% else %>
    <%# Marks current chosen option, if the opposite option is chosen, the record is updated to reflect the descion by the user %>
    <%= form_for [@likeable, @like] do |f| %>
        <% if @like.is_liked %>
            Currently Liked!
            <%= f.submit "Dislike" %>
        <% else %>
            <%= f.submit "Like" %>
            Currently Disliked!
        <% end %>
    <% end %>
<% end %>

所以最终,我只是想知道如何使从Section#show视图中对评论进行投票成为可能谢谢!

4

1 回答 1

1

试试这个:

<% @comments.each do |comment| %>
    <%= comment.content %>
    <%= render 'likes/like_button', :like => comment.like, :likeable => comment %>
<hr />
<% end %>

<% if like.nil? %>
    <%# No record of Like in table %>
    <%= form_for [likeable, Like.new] do |f| %>
    <%= f.submit "Like" %>
    <%= f.submit "Dislike" %>
    <% end %>
<% else %>
    <%# Marks current chosen option, if the opposite option is chosen, the record is updated to reflect the descion by the user %>
    <%= form_for [likeable, like] do |f| %>
        <% if like.is_liked %>
            Currently Liked!
            <%= f.submit "Dislike" %>
        <% else %>
            <%= f.submit "Like" %>
            Currently Disliked!
        <% end %>
    <% end %>
<% end %>
于 2012-06-30T00:29:44.077 回答