在块内渲染HTML-Ruby on Rails

时间:2018-06-19 13:41:51

标签: ruby-on-rails ruby

我正在尝试呈现一个带有部分内部块的HTML,但是唯一呈现的是最后四个封闭的</div>元素。

这是我的代码:

<%= new_item_modal do |i| %>
<div class="modal fade" id="newItemModal" tabindex="-1" role="dialog" aria-labelledby="newItemModal" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">New Item</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" id="new-item" style="overflow:scroll;height:500px;">
        <%= render partial: "/items/form", locals: { remote: true, item: @item, categories: @categories }%>
      </div>
    </div>
  </div>
</div>
<% end %>

还有我的new_item_modal函数:

def new_item_modal(&block)
   if current_location.present? && can_access("create", "items")
      @item = Item.new
      @categories = current_location.categories
      block.call
   end  
end

1 个答案:

答案 0 :(得分:0)

您的第一行应为<%= new_item_modal do |item, categories| %>,而您的new_modal_item方法应为block.call(@item, @categories)。确保重命名传递给部分变量的变量(例如,从@itemitem)。