【发布时间】:2015-09-15 00:17:14
【问题描述】:
我有一个 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 %>
但我仍然得到完全相同的错误。
编辑
为了帮助识别问题,如果我在部分视图中替换 @deal.deal_info[parameter_i_want_to_pass]['text1'] 到 @deal.deal_info[2]['text1'],那么它可以工作,所以问题必须真的是部分视图不想收到我在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)传递给局部视图。我查过了。
<%= 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'
【问题讨论】:
-
上述错误信息指的是哪个文件中的哪一行?我在您发布的代码中看不到您使用 modal_number 的位置。
标签: ruby-on-rails ruby json postgresql ruby-on-rails-4