我有一个 Deal 模型,它有一个名为“deal_info”的列/属性,它是一个 json 列。
例如看起来像这样
deal1.deal_info = [ { "modal_id": "4", "text1":"lorem" },
{ "modal_id": "6", "video2":"yonak" },
{ "modal_id": "9", "video2":"boom" } ]
deal2.deal_info = [ { "modal_id": "10", "text1":"lorem" },
{ "modal_id": "11", "video2":"yonak" },
{ "modal_id": "11", "image4":"boom" } ]
在我看来 deal.html.erb,我有:
<%= for deal_nb in 0..@deal.number_of_deals do %>
<div class="modal fade" id="myInfoModal<%= modal_nb %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<!-- render the right modal type -->
<%= render "layouts/modal_type_partials/mt#{ @deal.deal_info[deal_nb]['modal_id'] }", parameter_i_want_to_pass: deal_nb %>
</div>
<% end %>
在上面,正如您在上面看到的,我想为 parameter_i_want_to_pass 中循环的每次迭代传递迭代循环的次数(例如,第二次迭代将为 parameter_i_want_to_pass= 2)。
部分我有:
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">this is mt4</h4>
</div>
<div class="modal-body">
this is the text: <%= @deal.deal_info[parameter_i_want_to_pass]['text1'] %>
</div>
</div>
我收到以下错误:
no implicit conversion of String into Integer (on line "this is the text: <%= @deal.deal_info[parameter_i_want_to_pass]")
实际上,我什至试图通过传递一组数字而不是变量“deal_nb”来更轻松地检测错误
<%= render "layouts/modal_type_partials/mt#{ @deal.deal_info[deal_nb]['modal_id'] }", parameter_i_want_to_pass: 2 %>
但我仍然得到完全相同的错误。
编辑
为了帮助识别问题,如果我在局部 View 中替换 @deal.deal_info[parameter_i_want_to_pass]['text1'] 为 @deal.deal_info[2]['text1'],那么它会起作用,所以问题必须真的是部分 View 不想接收我在 deal.html.erb 里面设置的数字
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">this is mt4</h4>
</div>
<div class="modal-body">
this is the text: <%= @deal.deal_info[2]['text1'] %>
</div>
</div>
</div>
编辑 2 只是为了更新问题,我设法解决了部分问题。我使用方法 to_i 将字符串转换为数字
所以上面的代码现在可以工作了,它将信息 (parameter_i_want_to_pass =2) 传递给局部 View 。我检查过了。
<%= render "layouts/modal_type_partials/mt#{ @deal.deal_info[deal_nb]['modal_id'] }", parameter_i_want_to_pass: 2 %>
但剩下的问题,就是如何不设置为2而是传递迭代循环的次数
<%= for deal_nb in 0..@deal.number_of_deals do %>
<div class="modal fade" id="myInfoModal<%= modal_nb %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<!-- render the right modal type -->
<%= render "layouts/modal_type_partials/mt#{ @deal.deal_info[deal_nb]['modal_id'] }", parameter_i_want_to_pass: deal_nb %>
</div>
<% end %>
这里我遇到了一个错误
undefined local variable or method `modal_number'
最佳答案
您的变量名称有点令人困惑,但我认为这就是您想要做的。我正在使用 each_with_index
方法循环遍历 deal
的 deal_info
中的每个模式。然后,我将 locals: { }
参数用于 render partial:
,以便将变量传递给部分。然后部分引用这些变量,就像它们是在本地定义的一样。部分甚至不需要 index
变量,但我展示了无论如何您将如何传递它。
查看
<% @deal.deal_info.each_with_index do |modal, index| %>
<div class="modal fade" id="myInfoModal<%= index %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<!-- render the right modal type -->
<%= render partial: "layouts/modal_type_partials/mt#{ modal['modal_id'] }", locals: { modal: modal, index: index } %>
</div>
<% end %>
部分
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">this is mt4</h4>
</div>
<div class="modal-body">
this is the text: <%= modal['text1'] %>
</div>
</div>
...
关于ruby-on-rails - 将参数传递给局部 View - Rails 4/postgresql/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31100358/