简体   繁体   中英

Render a .js controller request as html

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM