1

我想在单击链接时将此代码从我的视图 html 中带入弹出窗口中这在 Ruby on Rails 中可能吗?我已经让弹出窗口工作了,但我想知道只显示评论的代码:

<div class = "comments"><% if post.comments.exists? %>
  <% post.comments.each do |comment| %>
  <%= image_tag("http://www.gravatar.com/someavatarlink %) <!-- Retrieves 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 %></div>

为 _comment_form.html.erb 添加了 Ajax 调用

 <%= link_to "Link", comment, :remote => true %>
      Comments
    <% end %></div></div> 
    <div id ="modal" class = "comments"><% if post.comments.exists? %>
      <% post.comments.each do |comment| %>
      <%= 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 %></div>

将 def show 添加到评论控制器中

class CommentsController < ApplicationController
     def new
    @post = post.new(params[:post])
    end

def show
  @comment = Comment.find(params[:id])
  respond_to do |format|
    format.js
  end

   def create
    @post = post.find(params[:micropost_id])
    @comment = Comment.new(params[:comment])
    @comment.post = @post
    @comment.user = current_user
    if @comment.save
       redirect_to(:back)
    else
      render 'shared/_comment_form'
    end
  end
end

创建 show.erb.js 并将其放入 'comments' 和 'shared' 文件夹

$("#popup").html('<%= escape_javascript(render "comments") %>');

然后最后写了我的部分内容,在 comments/_comment.html.erb

<% if post.comments.exists? %>
  <% post.comments.each do |comment| %>

    <%= 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 %>
4

1 回答 1

4

1. Ajax call

To retrieve the data you use an Ajax call.

<%= link_to "Link", comment, :remote => true %>

Rails will evaluate these requests and will look for a .js view first (it will use .html if it does not exist).

Make also sure that the controller accepts requests to .js like

def show
  @comment = Comment.find(params[:id])
  respond_to do |format|
    format.js
  end
end

2. write js view

Add a show.erb.js view to your Comments . This is a JavaScript file with ERB evaluation. In this template use your js popup code and tell it to fill a div with your html code like so:

$("#popup").html('<%= escape_javascript(render @comment) %>');

This will render the comment. The only thing we need then is a partial to render the html of the comment.

3. write partial for html

Write a partial for the view part you want to have in the popup. This can then be used in a normal html view or the js view. To make it work with the code above call it _comment.html.erb

To know more about partials you can check the guides here:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

于 2013-03-14T06:47:35.480 回答