处理 AJAX 回调呈现部分或重定向
Handle AJAX callback either render partial or redirect
我有一个 AJAX 调用来加载模式,但我有一个 before_action
如果 consent settings
关闭 redirect_to
设置页面。
AJAX 来电
$(document).on 'click', '#add-consent-form-template, #edit-consent-form-template', (e) ->
$.ajax(url: $(this).attr('data-url')).done (html) ->
$("#modal-consent-form-content").html html
$("#modal-consent-form").modal('show')
$('.date_picker').each ->
$(this).datepicker
showOn: 'both'
dateFormat: 'mm-dd-yy'
altFormat: 'yy-mm-dd'
altField: $(this).next()
minDate : 0
$('.ui-datepicker-trigger').addClass('fa fa-calendar fa-lg mar-left-10')
$('.description .widget').height($('.study-graph .widget').height())
e.preventDefault()
正常情况(如果同意设置开启)
render partial: 'add_consent_form_template_modal', layout: false
错误案例(如果同意设置关闭)
set_flash(error: 'Consent Forms are disabled. Please turn on Consent Forms first.')
redirect_to edit_study_path(id: @study)
问题
在redirect_to
情况下,它会在模态中呈现整个设置页面。如何处理 AJAX 回调以呈现部分内容或重定向到设置页面?
你在行动
写在下面的代码中,请根据您的要求添加if条件
if consent == ON
render partial: 'add_consent_form_template_modal', layout: false
else
set_flash(error: 'Consent Forms are disabled. Please turn on Consent Forms first.')
redirect_to edit_study_path(id: @study) and return
end
对于特定情况,当您的控件正在进行 AJAX 回调并且您想要 redirect_to
到 settings
并且不想在模态上呈现它时,只需传递 nil
作为 HTML.
set_flash(error: 'Consent Forms are disabled. Please turn on Consent Forms first.')
render html: nil
在你 Javascript/CoffeeScript 中,你可以简单地检查 length
你 html
。
$(document).on 'click', '#add-consent-form-template, #edit-consent-form-template', (e) ->
$.ajax(url: $(this).attr('data-url')).done (html) ->
if html.length > 0
$("#modal-consent-form-content").html html
$("#modal-consent-form").modal('show')
$('.date_picker').each ->
$(this).datepicker
showOn: 'both'
dateFormat: 'mm-dd-yy'
altFormat: 'yy-mm-dd'
altField: $(this).next()
minDate : 0
$('.ui-datepicker-trigger').addClass('fa fa-calendar fa-lg mar-left-10')
$('.description .widget').height($('.study-graph .widget').height())
else
window.location.href = <Link to your settings page>
e.preventDefault()
我有一个 AJAX 调用来加载模式,但我有一个 before_action
如果 consent settings
关闭 redirect_to
设置页面。
AJAX 来电
$(document).on 'click', '#add-consent-form-template, #edit-consent-form-template', (e) ->
$.ajax(url: $(this).attr('data-url')).done (html) ->
$("#modal-consent-form-content").html html
$("#modal-consent-form").modal('show')
$('.date_picker').each ->
$(this).datepicker
showOn: 'both'
dateFormat: 'mm-dd-yy'
altFormat: 'yy-mm-dd'
altField: $(this).next()
minDate : 0
$('.ui-datepicker-trigger').addClass('fa fa-calendar fa-lg mar-left-10')
$('.description .widget').height($('.study-graph .widget').height())
e.preventDefault()
正常情况(如果同意设置开启)
render partial: 'add_consent_form_template_modal', layout: false
错误案例(如果同意设置关闭)
set_flash(error: 'Consent Forms are disabled. Please turn on Consent Forms first.')
redirect_to edit_study_path(id: @study)
问题
在redirect_to
情况下,它会在模态中呈现整个设置页面。如何处理 AJAX 回调以呈现部分内容或重定向到设置页面?
你在行动
写在下面的代码中,请根据您的要求添加if条件
if consent == ON
render partial: 'add_consent_form_template_modal', layout: false
else
set_flash(error: 'Consent Forms are disabled. Please turn on Consent Forms first.')
redirect_to edit_study_path(id: @study) and return
end
对于特定情况,当您的控件正在进行 AJAX 回调并且您想要 redirect_to
到 settings
并且不想在模态上呈现它时,只需传递 nil
作为 HTML.
set_flash(error: 'Consent Forms are disabled. Please turn on Consent Forms first.')
render html: nil
在你 Javascript/CoffeeScript 中,你可以简单地检查 length
你 html
。
$(document).on 'click', '#add-consent-form-template, #edit-consent-form-template', (e) ->
$.ajax(url: $(this).attr('data-url')).done (html) ->
if html.length > 0
$("#modal-consent-form-content").html html
$("#modal-consent-form").modal('show')
$('.date_picker').each ->
$(this).datepicker
showOn: 'both'
dateFormat: 'mm-dd-yy'
altFormat: 'yy-mm-dd'
altField: $(this).next()
minDate : 0
$('.ui-datepicker-trigger').addClass('fa fa-calendar fa-lg mar-left-10')
$('.description .widget').height($('.study-graph .widget').height())
else
window.location.href = <Link to your settings page>
e.preventDefault()