我想在一些标准操作上调用partials。我正在使用此方法来调用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
我怎么能拥有这样的东西:
link_to 'Delete', '#', :partial => 'layouts/delete', :remote => :true?
所以我只在点击时渲染部分而不是在页面加载时渲染?
答案 0 :(得分:29)
您可以使用javascript执行此操作:
<%= link_to "Delete", delete_content_path, :remote => true %>
相应控制器中的操作将是:
我的控制器:
def delete_content
respond_to do |format|
format.js
end
end
然后,您可以在链接的正确目录中创建delete_content.js.erb
,并在其中输入以下代码:
<强> 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 放在同一个文件夹中。
答案 1 :(得分:15)
要添加到接受的答案,我只是在将js部分更改为以下内容后才能使用它:
$('#div_id').html("<%= escape_javascript(render :partial => 'my_partial') %>");
如果没有escape_javascript
,它只是在背景中呈现部分而不是更新视图。
答案 2 :(得分:0)
执行此操作:
link_to "Delete #{item}", '/model/confirm_deletion', :method => :delete, :remote => true #add the class and extra attributes if neeeded
控制器上的
def confirm_deletion
end
并为js
中的confirm_deletion操作添加一个视图#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
:javascript
$(body).append($('#RevealDelete'));
这将使ajax请求加载该自定义确认对话框,也许你想添加一些包装器来插入对话框而不是使用body.append