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") %>');

その後、最終的にコメント/_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