【问题标题】:Rails UJS cannot render image_pack_tagRails UJS 无法呈现 image_pack_tag
【发布时间】:2022-12-01 08:39:51
【问题描述】:

它看起来微不足道,但 rails 无法正确解析此 .js.erb 文件:

$('.username-content').fadeOut('slow', function() {
  var div = $("\
    <div class='username-content'>\
      <%= t('onboarding_username_done') %>\
      <div class='row'>\
          <div class='col-md-6 offset-md-3'>\
            <%= image_pack_tag('badge-prety.gif', class: 'rounded') %>\
            <button type='button' class='btn btn-primary mt-3 username'><%= t('next') %></button>\
          </div>\
      </div>\
    </div>\
  ").hide();
  $(this).replaceWith(div);
  $('.username-content').fadeIn('slow');
});

我可能遇到了渲染问题:

不确定如何进行。有什么线索吗?

【问题讨论】:

  • 我建议将所有 HTML 移动到一个部分,然后你可以做 var div = $("&lt;%= j(render partial: "that_partial") %&gt;")

标签: ruby-on-rails ujs rails-ujs


【解决方案1】:

最好的方法是为此使用部分,例如: 使用 html 内容在 partials 文件夹中创建一个名为 username_content 的部分,并将代码替换为:

$('.username-content').fadeOut('slow', function() {
  $(this).replaceWith("<%= j render 'partials/username_content' %>");
  $('.username-content').fadeIn('slow');
});

https://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-escape_javascript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    相关资源
    最近更新 更多