我正在努力將ruby插值嵌入到html字符串的字符串變量中。 我想這聽起來有些混亂,希望代碼能幫助您理解:
def devise_error_messages!
return '' if resource.errors.empty?
messages = resource.errors.full_messages
html = <<-HTML
<script type='text/javascript'>
toastr.options = {
'closeButton': true,
'debug': false,
'newestOnTop': false,
'progressBar': false,
'positionClass': 'toast-bottom-left',
'preventDuplicates': true,
'onclick': null,
'showDuration': '3000',
'hideDuration': '1000',
'timeOut': '5000',
'extendedTimeOut': '1000',
'showEasing': 'swing',
'hideEasing': 'linear',
'showMethod': 'fadeIn',
'hideMethod': 'fadeOut'
}
#{ messages.each do |m| }
toastr['error']("#{m}");
#{end}
</script>
HTML
html.html_safe
end
因此,如您所見,我正在嘗試遍歷messages
數組並為每條消息生成以下js代碼行: toastr['error']("#{m}");
您能幫我正確實施嗎?