我想在单击链接时将此代码从我的视图 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 %>