Rails 4:我可以将一个部分传递给一个部分吗?
Rails 4: Can I pass a partial into a partial?
我找到了很多关于如何将数据、json、字符串等传递到一个部分的信息,但我想将一个部分传递到另一个部分。到目前为止,这是我的代码:
我的parent.html.erb
<%= render partial: 'shared/faq_question', locals: {
id: '1',
question: 'A question I\'m asking',
answer: 'A string answer that does\'nt cause any problems'
} %>
<%= render partial: 'shared/faq_question', locals: {
id: '2',
question: 'A question that requires an HTML answer',
answer: 'THE PARTIAL SHOULD GO HERE'
} %>
</ul>
我的_faq_question.html.erb
<li id="question<%= id %>">
<a href="#question<%= id %>" class="ca" data-activate="#question<%= id %>" data-method2="toggle">
<i class="close-icon"></i>
<span><%= question %></span>
</a>
<div>
<p><%= answer %></p>
</div>
</li>
到目前为止,还不错。上面的第二个问题的内容包含 HTML 内容,如下所示:
<%= render partial: 'shared/faq_question', locals: {
id: '22',
question: 'A question that requires an HTML answer?',
answer: "<span>Something</span>
<ul>
<li>Some foo</li>
<li>Some bar</li>
<li>Content</li>
</ul>
<span>Keep in mind:</span>
<ul>
<li>There is more </li>
<li>This is great</li>
</ul>
<span>Please note:</span>
And another sentence".html_safe
} %>
我想用部分答案替换 answer。
我的_answer.html.erb部分
<span>Something</span>
<ul>
<li>Some <%= foo %></li>
<li>Some bar <%= foofoo %></li>
<li>Content <%= bar %></li>
</ul>
<span>Keep in mind:</span>
<ul>
<li>There is more </li>
<li>This is great</li>
</ul>
<span>Please note:</span>
And another sentence
我尝试添加部分代码段并使用 html_safe 但是当浏览器呈现存在语法错误时,这会中断并出现错误,这是因为部分答案没有正确转义。
我试过的:
answer: <%= render partial: 'shared/faq_question' %>.html_safe
和
answer: (<%= render partial: 'shared/faq_question' %>).html_safe
和
answer: "<%= render partial: 'shared/faq_question' %>".html_safe
我需要做什么才能正确转义部分内容并使其在浏览器中呈现?
我还想将数据传入第二部分以替换以下变量:
<li>Some <%= foo %></li>
<li>Some bar <%= foofoo %></li>
<li>Content <%= bar %></li>
foo
foofoo
栏
任何正确方向的帮助或想法将不胜感激。
谢谢!
您已经在 ruby 块中,不需要额外的 erb-tags,试试
answer: render(partial: 'shared/faq_question')
我找到了很多关于如何将数据、json、字符串等传递到一个部分的信息,但我想将一个部分传递到另一个部分。到目前为止,这是我的代码:
我的parent.html.erb
<%= render partial: 'shared/faq_question', locals: {
id: '1',
question: 'A question I\'m asking',
answer: 'A string answer that does\'nt cause any problems'
} %>
<%= render partial: 'shared/faq_question', locals: {
id: '2',
question: 'A question that requires an HTML answer',
answer: 'THE PARTIAL SHOULD GO HERE'
} %>
</ul>
我的_faq_question.html.erb
<li id="question<%= id %>">
<a href="#question<%= id %>" class="ca" data-activate="#question<%= id %>" data-method2="toggle">
<i class="close-icon"></i>
<span><%= question %></span>
</a>
<div>
<p><%= answer %></p>
</div>
</li>
到目前为止,还不错。上面的第二个问题的内容包含 HTML 内容,如下所示:
<%= render partial: 'shared/faq_question', locals: {
id: '22',
question: 'A question that requires an HTML answer?',
answer: "<span>Something</span>
<ul>
<li>Some foo</li>
<li>Some bar</li>
<li>Content</li>
</ul>
<span>Keep in mind:</span>
<ul>
<li>There is more </li>
<li>This is great</li>
</ul>
<span>Please note:</span>
And another sentence".html_safe
} %>
我想用部分答案替换 answer。
我的_answer.html.erb部分
<span>Something</span>
<ul>
<li>Some <%= foo %></li>
<li>Some bar <%= foofoo %></li>
<li>Content <%= bar %></li>
</ul>
<span>Keep in mind:</span>
<ul>
<li>There is more </li>
<li>This is great</li>
</ul>
<span>Please note:</span>
And another sentence
我尝试添加部分代码段并使用 html_safe 但是当浏览器呈现存在语法错误时,这会中断并出现错误,这是因为部分答案没有正确转义。
我试过的:
answer: <%= render partial: 'shared/faq_question' %>.html_safe
和
answer: (<%= render partial: 'shared/faq_question' %>).html_safe
和
answer: "<%= render partial: 'shared/faq_question' %>".html_safe
我需要做什么才能正确转义部分内容并使其在浏览器中呈现?
我还想将数据传入第二部分以替换以下变量:
<li>Some <%= foo %></li>
<li>Some bar <%= foofoo %></li>
<li>Content <%= bar %></li>
任何正确方向的帮助或想法将不胜感激。 谢谢!
您已经在 ruby 块中,不需要额外的 erb-tags,试试
answer: render(partial: 'shared/faq_question')