我有一个名为'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" } ]
在我的view deal.html.erb上,我有:
<%= for deal_nb in [email protected]_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 %>
上面,如您所见,我想为参数“我要”中的循环的每次迭代传递迭代循环的编号(例如,第二次迭代将是参数“我要”传递=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\u info[parameter\u I\u want\u pass]['text1'][email protected]\u 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
为了更新问题,我设法解决了一部分问题。我用这个方法把字符串转换成一个数字
因此,上面的代码现在可以工作了,它将信息(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 [email protected]_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:
参数,以便将变量传递给partial。然后部分引用这些变量,就像它们是在本地定义的一样。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/