问题
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