8

我想在一些标准操作上调用部分。我正在使用这种方法来调用部分:

 %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? 

这样我只在单击而不是在页面加载时呈现该部分?

4

3 回答 3

30

您可以使用 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放在同一个文件夹中。

于 2013-03-02T13:15:57.843 回答
17

要添加到接受的答案,我只有在将 js 部分更改为以下内容后才能使用它:

$('#div_id').html("<%= escape_javascript(render :partial => 'my_partial') %>");

没有escape_javascript它,它只是在后台渲染部分而不是更新视图。

于 2013-11-16T06:57:52.223 回答
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

于 2013-03-02T13:14:03.840 回答