如何从特定页面上的渲染中删除布局application.html.erb.它现在在我的应用程序的所有页面上都可见.
我正在使用3.0.0.beta3构建一个新的应用程序.我只是尝试将js.erb模板呈现给Ajax请求以执行以下操作(在publications_controller.rb中):
def get_pubmed_data
entry = Bio::PubMed.query(params[:pmid])# searches PubMed and get entry
@publication = Bio::MEDLINE.new(entry) # creates Bio::MEDLINE object from entry text
flash[:warning] = "No publication found."if @publication.title.blank? and @publication.authors.blank? and @publication.journal.blank?
respond_to do |format|
format.js
end
end
Run Code Online (Sandbox Code Playgroud)
目前,我的get_pubmed_data.js.erb模板很简单
alert('<%= @publication.title %>')
Run Code Online (Sandbox Code Playgroud)
服务器正在响应以下内容
alert('Evidence for a herpes simplex virus-specific factor controlling the transcription of deoxypyrimidine kinase.')
Run Code Online (Sandbox Code Playgroud)
不同之处在于没有在浏览器中发生的,可能是因为内容类型的响应是不是如图这里部分转载响应头"文/ JavaScript的""text/html的",这是完美的罚款:
Status 200
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html; charset=utf-8
Run Code Online (Sandbox Code Playgroud)
这是一个错误还是我错过了什么?谢谢你的帮助!
我使用了在 Rails 中使用 Javascript指南中的remote: true习语:
# new.html.slim
= form_for @thing, remote: true do |f|
f.text_field :whatever
f.submit 'Submit'
Run Code Online (Sandbox Code Playgroud)
# thing_controller.rb
layout 'foo'
def create
end
Run Code Online (Sandbox Code Playgroud)
# create.js.erb
alert('foobar')
Run Code Online (Sandbox Code Playgroud)
这失败了,因为由于create.js.erb某种原因在 'foo' 布局内呈现并返回为 html,而不是 javascript,尽管该请求被正确处理为 Javascript:
Processing by ThingsController#create as JS
Parameters: {"utf8"=>"?", "commit"=>"Submit"}
Rendered things/create.js.erb (0.6ms)
Run Code Online (Sandbox Code Playgroud)
(无论我respond_to在控制器操作中是否有显式格式块,问题都是一样的。)
如此处和此处所述,包括render layout: false在控制器操作中解决了问题:
# thing_controller.rb
layout 'foo'
def create
render layout: false
end
Run Code Online (Sandbox Code Playgroud)
但为什么我需要render layout: false这里?为什么 Rails 在 …