单击时呈现局部 [英] render partial on click
问题描述
我想在一些标准操作上调用局部函数.我正在使用这种方法来调用局部的:
I would like to call partials on some standard operations. I am using this method for calling the partial:
%li= link_to 'Delete Event', 'javascript:void(0);', :class => 'alert tiny button', :data => {'reveal-id' => :RevealDelete}
= render 'layouts/reveal_delete', :item => event_display(@event.event), :resource => @event
然后在我的局部
#RevealDelete.reveal-modal
%a.close-reveal-modal ×
%h3= "Delete #{item}"
%p Are you sure you want to delete this?
=link_to "Delete #{item}", resource, :method => :delete, :remote => :true, :confirm => resource, :class => 'button close-reveal-modal'
%a.button.alert.close-reveal-modal Cancel
我怎么能拥有这样的东西?
How can I have this has as something like:
link_to 'Delete', '#', :partial => 'layouts/delete', :remote => :true?
这样我只在单击时渲染该部分,而在页面加载时不渲染?
so that I only render that partial when clicked and not when the page loads?
推荐答案
您可以使用javascript这样的代码:
You can do that with javascript like:
<%= link_to "Delete", delete_content_path, :remote => true %>
相应控制器中的操作将是这样:
The action in your corresponding controller then will be this:
我的控制器:
def delete_content
respond_to do |format|
format.js
end
end
然后,您可以在链接的正确目录内创建delete_content.js.erb
,并在其中放置以下代码:
Then you can create the delete_content.js.erb
inside your correct directory of the link and there you put the following code:
delete_content.js.erb
$('#div_id').html("<%= render :partial => 'my_partial' %>");
然后在您看来:
delete_content.html.erb
<div id = "div_id">
#this div is html div that will render your partial
</div>
请不要忘记将部分 _my_partial.html.erb 放在同一文件夹中.
Don't forget to put your partial _my_partial.html.erb in the same folder.
这篇关于单击时呈现局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!