Render a .js controller request as html

情到浓时终转凉″ 提交于 2019-12-06 05:11:36

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/10607369/render-a-js-controller-request-as-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!