我在rails应用程序中的最后一步,如何在弹出窗口中显示注释?

时间:2013-03-14 00:32:10

标签: ruby ruby-on-rails-3 html5 facebox

这是完成我的第一个rails应用程序的最后剩余项目,需要一些帮助。 在每个用户配置文件(localhost:3000 / users / username)上,列出了用户发布的帖子。与每个帖子相关的都是评论。所以post_id:3可以有评论。

我已经在视图表单中工作,但是当点击每个帖子下的“评论”链接时,我需要在弹出窗口中显示评论。 我已经应用了facebox,这是一个基于jQuery的灯箱,可以显示弹出窗口。

我只需要将show.html.erb中当前显示的内容移动到弹出窗口中。

_comment_form.html.erb呈现_post.html.erb

  <%= link_to #, :rel => "facebox-#{post.id}" do %>
  +<%= post.comments.count.to_s %>
<% end %>
 <div class ="ItemComments"><% if post.comments.exists? %>
   <% post.comments.each do |comment| %>
   <%= image_tag("http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(comment.user.email)}" %>
   <span class="users"><%= link_to comment.user.name, comment.user %></span>
   <span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
   <span class="content2"><%= comment.comment_content %></span>
   <% end %>
 <% end %></div>

上面使用:

呈现到_post.html.erb
<%= render 'shared/comment_form', post: post if signed_in?%>

然后它呈现为show.html.erb

我正在尝试使用此行,但我将其链接到哪个?

    <%= link_to #, :rel => "facebox-#{post.id}" do %>
  +<%= post.comments.count.to_s %>
<% end %>

这是共享/ _comment.html.erb

<% if post.comments.exists? %>
  <% post.comments.each do |comment| %>
  <%= image_tag("http://www.gravatar.com/avatar.php?gravatar") %>
    <%= link_to comment.user.name, comment.user %>
            <span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
    <span class="content2"><%= comment.comment_content %></span>
    <% end %>
<% end %>

1 个答案:

答案 0 :(得分:0)

这样做的一种方法是将您的注释呈现为隐藏的div并将该div设为id。接下来,使用#后跟id将链接指向div的id。它看起来像这样:

_post.html.erb

<%= link_to "#comments", :rel => "facebox" do %>
    <%= post.comments.count.to_s %>
<% end %>
<div id="comments">
    <%= render 'shared/comment_form', post: post if signed_in?%>
</div>

CSS

#comments {
    display: none;
}

请参阅Facebox docs上的“Divs”标题。