[英]Render partial in a div on clicking a link
我有一个像这样的侧边栏;
<div id = "sidebar-links">
<% @locations.each do |locs| %>
<p style = "border-bottom: 1px black solid">
<a href = "#" class = "sidebar-click"><%= locs['Address'] %></a></p>
<% end %>
</div>
我想在点击其中一个侧栏链接并且还想将locs
作为局部变量传递给该局部时,在另一个div id = "container"
渲染部分;
我该怎么做呢?
首先尝试使用link_to
helper方法:
<%= link_to locs['Address'], ajax_link, :remote => true, :html => {:class => "sidebar-click"} %>
然后你应该创建一个js绑定来处理ajax响应,如:
$(function() {
$(".sidebar-click").bind("ajax:success",
function(event, data){
$("#container").html(data);
});
});
更新:请查看以下文档: http : //apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote
试试这个
<%= link_to locs['Address'], "path/to/redirect", :class => "side_bar" %>
然后在js
$(".document").ready(function(){
$(".side_bar").live("click", function(){
$.ajax({
dataType: 'html',
type: 'get',
url: $(this).attr('href'),
success: function(data){
$(target_div).html(data);
}
})
})
})
并在你的控制器动作
def action
#write your code here
respond_to do |format|
format.js{render: template}
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.