在执行 AJAX HTTP 请求时渲染没有布局的模板时出现问题

发布于 2024-12-03 05:10:24 字数 983 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-12-10 05:10:24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文