为什么我的Rails控制器操作中需要`render layout:false`? [英] Why do I need `render layout: false` in my Rails controller action?

查看:90
本文介绍了为什么我的Rails控制器操作中需要`render layout:false`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用<$ href =http://guides.rubyonrails.org/working_with_javascript_in_rails.html =nofollow中的 remote:true idiom noreferrer>使用Rails中的Javascript 指南:

I'm use the remote: true idiom from the Working with Javascript in Rails guide:

# new.html.slim
= form_for @thing, remote: true do |f|
  f.text_field :whatever
  f.submit 'Submit'





# thing_controller.rb
layout 'foo'

def create
end





# create.js.erb
alert('foobar')

此操作失败,因为 create.js.erb 由于某种原因在'foo'布局中呈现并以html而不是javascript的形式返回,尽管请求被正确处理为Javascript:

This fails, because create.js.erb is for some reason rendered inside the 'foo' layout and returned as html, not javascript, despite the fact that the request is correctly processed as Javascript:

Processing by ThingsController#create as JS
  Parameters: {"utf8"=>"✓", "commit"=>"Submit"}
  Rendered things/create.js.erb (0.6ms)

(无论我是否在控制器操作中有明确的 respond_to 格式块,问题都是一样的。)

(The problem is the same whether or not I have an explicit respond_to format block in the controller action.)

如上所述此处此处,在控制器操作中包含渲染布局:false 修复了问题:

As noted here and here, including render layout: false in the controller action fixes the problem:

# thing_controller.rb
layout 'foo'

def create
  render layout: false
end

但为什么我需要渲染布局:false 这里?为什么Rails在html布局中呈现javascript?我特别感到困惑,因为我在其他几个地方使用过相同的习惯用语,之前从未遇到过这个问题。

But why do I need render layout: false here? Why is Rails rendering the javascript inside an html layout? I'm particularly puzzled because I've used the same idiom in several other places and never run into this problem before.

推荐答案

rails中的动作控制器默认响应HTML响应(除非另有说明)。

The action controller in rails responds with HTML response by default (unless otherwise instructed).

layout'foo'强制执行使用 app / views / layouts / foo.html.slim 作为视图文件的模板。所以与 thing_controller.rb 上的操作相关的所有视图都在布局'foo'中呈现,最终的HTML使用布局'foo'和视图文件<$ c生成$ c> create.html.slim 默认发送回客户端。

layout 'foo' enforces the use of app/views/layouts/foo.html.slim as the template for your view files. so all the views associated with the actions on your thing_controller.rb are rendered inside the layout 'foo' and the final HTML generated with layout 'foo' and view file create.html.slim is sent back to the client by default.

如果要强制返回js模板而不是HTML文件,你需要在你的动作中明确定义它:

If you want to enforce returning js template instead of HTML file, you need to explicitly define it in your action like this:

# thing_controller.rb
layout 'foo'

def create
  respond_to do |format|
    # use :template if your view file is somewhere else than rails convention
    format.js {
      :template => "somewhere/create.js.erb", 
      :layout => false
    }
  end
end

其中渲染布局:false 强制执行rails NOT寻找任何布局文件来包装你的视图文件(即rails引擎只处理 create.js.erb 在'foo'布局中没有定义HTML标头的文件),用于将其发送回客户端。

where render layout: false enforces rails NOT TO look for any layout file to wrap your view file (i.e the rails engine just processes create.js.erb file without HTML headers defined on your 'foo' layout) for sending it back to client.

这篇关于为什么我的Rails控制器操作中需要`render layout:false`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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