在执行 AJAX HTTP 请求时渲染没有布局的模板时出现问题
我正在使用 Ruby on Rails 3.0.10 和 jQuery 1.6。我试图在控制器操作中处理与模板渲染相关的行为。我正在考虑根据参数请求渲染或不渲染布局(有关更多信息,请参阅下面的代码)。
在我的控制器中,响应程序是这样实现的:
def show
respond_to do |format|
format.html { render :layout => params[:layout] } # I am using the 'params[:layout]' (boolean) in order to handle the layout rendering.
end
end
因此,如果我浏览 http://website.com/articles/1
URL,它会加载视图模板,没有布局。
现在,如果我执行如下所示的 AJAX HTTP 请求,
$jQ.ajax({
type: 'GET',
url: '<%= article_url(@article) %>',
data: 'layout=false', # Here I set the parameter 'params[:layout]' so to not render the 'layout' in the related controller action
error: function(jqXHR, textStatus, errorThrown) {
...
},
success: function(data, textStatus, jqXHR) {
...
div_jquery_object.html(data);
}
});
它会加载视图模板和布局。
AJAX HTTP 请求填充是否应该返回不带布局的模板?我该如何制作?
I am using Ruby on Rails 3.0.10 and jQuery 1.6. I am trying to handle in a controller action the behavior related to the template rendering. I am considering to render or not render a layout based on a parameter request (see the below code for more information).
In my controller the responder is implemented in this way:
def show
respond_to do |format|
format.html { render :layout => params[:layout] } # I am using the 'params[:layout]' (boolean) in order to handle the layout rendering.
end
end
So, if I browse the http://website.com/articles/1
URL it loads the view template without the layout.
Now, if I perform an AJAX HTTP request like the following
$jQ.ajax({
type: 'GET',
url: '<%= article_url(@article) %>',
data: 'layout=false', # Here I set the parameter 'params[:layout]' so to not render the 'layout' in the related controller action
error: function(jqXHR, textStatus, errorThrown) {
...
},
success: function(data, textStatus, jqXHR) {
...
div_jquery_object.html(data);
}
});
it loads the view template with the layout.
Should the AJAX HTTP request populate return the template without the layout? How can I make ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
params[:layout]
是一个字符串。如果您不需要任何布局,则应该传递
false
,而不是"false"
。所以你应该使用一些
case
语句。params[:layout]
is a string.If you don't want any layout, you should pass
false
, not"false"
.So you should use some
case
statement.