渲染一个.js控制器请求为html [英] Render a .js controller request as html

查看:131
本文介绍了渲染一个.js控制器请求为html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的before_filter 在我的Rails应用程序发送用户 LOGIN_URL 如果他们退出时,他们提交一个请求(HTML或JS格式)。

我想我的 format.js 来产生一个相同的结果的format.html ,在以下情况下,渲染与通知布局视图。我怎样才能做到这一点?

 的respond_to办|格式|
  format.js
  的format.html {渲染:布局=> 注意 }
结束
 

解决方案

您可以强制使用的格式是这样的:

 的respond_to办|格式|
  format.js {渲染:布局=> 通知:格式=> [:HTML]}
  的format.html {渲染:布局=> 注意 }
结束
 

编辑:

您需要的是文档的某些部分被替换的响应。

:这是一个JavaScript的,没有工作做回应

在你的控制器:

 的respond_to办|格式|
  format.js
  的format.html {渲染:布局=> 注意 }
结束
 

在你login.js查看:

  $('#不管')HTML('<%= escape_javascript(渲染:登录,格式:[:HTML])%GT;')。
 

...或者类似的东西。

I have a before_filter in my Rails app that sends users to login_url if they are logged out when they submit a request (in either html or js format).

I would like my format.js to produce an identical result to format.html, in the following case to render the view with the 'notice' layout. How can I do this?

respond_to do |format|
  format.js
  format.html{ render :layout => "notice" }
end

解决方案

you can force the usable formats like this :

respond_to do |format|
  format.js   { render :layout => "notice", :formats => [:html] }
  format.html { render :layout => "notice" }
end

EDIT:

What you need is some part of the document being replaced by the response. This is done by responding with a javascript that does the job:

In your controller :

respond_to do |format|
  format.js
  format.html { render :layout => "notice" }
end

In your login.js view :

$('#whatever').html('<%= escape_javascript( render :login, formats: [ :html ]) %>')

... or something similar

这篇关于渲染一个.js控制器请求为html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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