Rails的只有AJAX请求负载查看HTML [英] Rails ajax request load view html only

查看:126
本文介绍了Rails的只有AJAX请求负载查看HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用Rails和jQuery的AJAX的web页面。那我遇到的问题是,当我尝试使用jQuery的AJAX方法加载一个视图到一个div我得到我的应用程序布局返回的观点。有没有一种方法只检索显示数据,如果它是一个Ajax请求,但加载我的应用程序布局,以及如果它不是一个AJAX请求?

I'm trying to make an AJAX enabled web page using rails and jQuery. The problem that I am running into is that when I try and load a view into a div using jQuery's AJAX method I am getting my application layout returned with the view. Is there a way to only retrieve the view data if it's an AJAX request, but load my application layout as well if its not an AJAX request?

下面的控制器方法我打电话:

Here's the controllers method that I am calling:

def new
   @blog = Blog.new

   respond_to do |format|
       format.html # Goes to the new.html.erb view.
       format.xml { render :xml => @blog }
   end
end

感谢您的帮助。

Thanks for the help.

推荐答案

将此行添加到您的的respond_to 块:

format.js { render :layout => false }

这表示,对JavaScript的请求,不会使布局。现在,它应该为Ajax请求,和普通的网页链接。

This says that for javascript requests, don't render the layout. Now it should work for both ajax requests, and regular web links.

这篇关于Rails的只有AJAX请求负载查看HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆