简体   繁体   English

在bootstrap popover rails 5 app中渲染部分?

[英]Rendering partial in bootstrap popover rails 5 app?

I'm having a problem rendering a partial in a bootstrap popover in my rails app. 我在我的rails应用程序中在引导程序弹出窗口中呈现部分问题。

The partial is always rendered as a plain text( showing all the HTML tags etc). partial总是呈现为纯文本(显示所有HTML标记等)。

this is the code from the index.html.erb 这是index.html.erb中的代码

<span class="has-popover"
      style="cursor:pointer;"
      data-toggle="popover"
      data-trigger="hover"
      data-container="body"
      data-placement="right"
      title="Lorem Ipsum"
      data-content= "<%= render :partial => 'envs/e1' %>" >
      <i class="fa fa-question-circle "  aria-hidden="true"></i>
</span>

In the app.js I have this snippet app.js我有这个片段

$(".has-popover").popover({
    html : true
});

and this is the _e1.html.erb partial in the envs folder 这是envs文件夹中的_e1.html.erb部分

<h2>Mauris euismod sollicitudin?</h2>

<p>Morbi sit amet tellus pellentesque, maximus eros a, aliquam nunc. Vivamus velit velit, vestibulum at eros eu, iaculis hendrerit tortor. Morbi ullamcorper purus at ornare ullamcorper. </p>

<br>

<p>Morbi sit amet tellus pellentesque, maximus eros a, aliquam nunc. Vivamus velit velit, vestibulum at eros eu, iaculis hendrerit tortor. Morbi ullamcorper purus at ornare ullamcorper. </p>

I have wrapped "<%= render :partial => 'envs/e1' %>" this line in both raw() and html_safe without any luck. 我在raw()html_safe包含了"<%= render :partial => 'envs/e1' %>"这一行而没有任何运气。

* ADDED EXAMPLES * below are examples on how I've been using html_safe and raw in the snipped *以下添加的示例*是关于我如何在html_safe使用html_saferaw示例

data-content= raw("<%= render :partial => 'envs/e1' %>") - text appears the "right" way but outsite the popover. data-content= raw("<%= render :partial => 'envs/e1' %>") - 文本以“正确”的方式显示,但在弹出窗口外。

data-content= "<%= raw(render :partial => 'envs/e1') %>" > - text appears as plain-text data-content= "<%= raw(render :partial => 'envs/e1') %>" > - 文本显示为纯文本

data-content= "<%= render :partial => raw('envs/e1') %>" > - text appears as plain-text data-content= "<%= render :partial => raw('envs/e1') %>" > - 文本显示为纯文本

data-content= "<%= render :partial => 'envs/e1' %>".html_safe - text appears as plain-text data-content= "<%= render :partial => 'envs/e1' %>".html_safe - 文本显示为纯文本

data-content= "<%= render :partial => 'envs/e1'.html_safe %>" - text appears as plain-text data-content= "<%= render :partial => 'envs/e1'.html_safe %>" - 文本显示为纯文本

there must be some way to have the partial styled inside the popover?? 必须有一些方法让部分样式在popover内? Or am I doing this all wrong? 或者我这样做是错的?

please advise me thanks in advance. 请提前告知我。

I believe that you have to enable data-html = "true" in your popover span. 我相信你必须在你的popover范围内启用data-html = "true" At least it worked at my machine. 至少它在我的机器上工作。

So it would be written like: 所以它会写成:

<span class="has-popover"
  style="cursor:pointer;"
  data-toggle="popover"
  data-trigger="hover"
  data-html="true" <!-- This is what you have to add to the code -->
  data-container="body"
  data-placement="right"
  title="Lorem Ipsum"
  data-content= "<%= render :partial => 'envs/e1' %>" >

By default, Bootstrap popovers don't accept html entered into them by automatically inheriting the option: data-html="false" you need to add and change it to true . 默认情况下,Bootstrap popovers不接受通过自动继承选项data-html="false"data-html="false"您需要添加并将其更改为true

If you want to read more about what options you can pass through to bootstrap popovers, check out this section of their API and you can review what you can potentially do with it. 如果您想了解更多关于可以传递给bootstrap popovers的选项的信息,请查看其API的这一部分,您可以查看可以使用它的内容。

Your Bootstrap markup and JS look fine, so the issue is almost certainly that your view is outputting escaped HTML rather than the markup that you need. 您的Bootstrap标记和JS看起来很好,因此几乎可以肯定的是,您的视图输出的是转义的HTML而不是您需要的标记。

Your attempts with html_safe are very close and definitely on the right track, but the lack of brackets means that it's not quite doing what you think. 你对html_safe尝试非常接近并且肯定是在正确的轨道上,但缺少括号意味着它并没有完全按你的想法行事。 Try this instead: 试试这个:

data-content="<%= render(partial: 'envs/e1').html_safe %>"

Note that the html_safe is applied to the output of render , and the outer quotes are retained in the HTML and not interpreted by Rails. 请注意, html_safe应用于render的输出,外引号保留在HTML中,不由Rails解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM