I have a simple scenario where I want to request a page. The request format is text/html. If there is some error in the controller/action logic for that request, I want to get an error message. The twist is that I want this error message to be communicated to be with a javascript response type (ie no page refresh). If there are no errors, then I want the page to be loaded via the expected text/html response type.
Basically, I want two different response types depending on whether there is an error or not. Is this doable in rails 3? If so, what is best practice?
A quick code sample would be much appreciated.
Thanks.
Sure it's doable!
I would do it like this:
def some_action
# code
# more code
# implicit or explicit rendering of an html template
rescue Exception => ex
render :json => ex.to_json,
:content_type => 'application/json',
:layout => false
end
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.