jQuery 单击时呈现部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15174798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
render partial on click
提问by Boss Nass
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
Then in my partial,
然后在我的部分,
#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?
这样我只在点击时呈现部分而不是在页面加载时呈现?
回答by My God
You can do that with javascript like:
您可以使用 javascript 来做到这一点,例如:
<%= link_to "Delete", delete_content_path, :remote => true %>
The action in your corresponding controller then will be this:
您相应控制器中的操作将是这样的:
My Controller:
我的控制器:
def delete_content
respond_to do |format|
format.js
end
end
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
在链接的正确目录中创建内部,并在其中放置以下代码:
delete_content.js.erb
delete_content.js.erb
$('#div_id').html("<%= render :partial => 'my_partial' %>");
Then in your view:
那么在你看来:
delete_content.html.erb
delete_content.html.erb
<div id = "div_id">
#this div is html div that will render your partial
</div>
Don't forget to put your partial _my_partial.html.erbin the same folder.
不要忘记将您的部分_my_partial.html.erb放在同一个文件夹中。
回答by Alex Onsager
To add to the accepted answer, I only got it to work after changing the js portion to the following:
要添加到已接受的答案中,我仅在将 js 部分更改为以下内容后才使其工作:
$('#div_id').html("<%= escape_javascript(render :partial => 'my_partial') %>");
Without the escape_javascript
it was just rendering the partial in the background and not updating the view.
没有escape_javascript
它只是在后台渲染部分而不更新视图。
回答by arieljuod
on the view do this:
在视图上这样做:
link_to "Delete #{item}", '/model/confirm_deletion', :method => :delete, :remote => true #add the class and extra attributes if neeeded
on your controller
在您的控制器上
def confirm_deletion
end
and add a view to the confirm_deletion action in js
并在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'));
that would make an ajax request to load that custom confirmation dialog, maybe you want to add some wrapper to insert the dialog instead of using body.append
这将发出 ajax 请求以加载该自定义确认对话框,也许您想添加一些包装器来插入对话框而不是使用 body.append