Rails 4:我可以将部分传递给部分吗?

时间:2015-12-08 16:36:41

标签: ruby-on-rails ruby-on-rails-4 partials

我发现了很多关于如何将数据,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.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

任何有关正确方向的帮助或想法都将受到高度赞赏。 谢谢!

1 个答案:

答案 0 :(得分:2)

您已经在ruby块中,无需额外的erb-tag,请尝试

answer: render(partial: 'shared/faq_question')