将参数传递给局部视图 - Rails 4 / postgresql / json

时间:2015-06-28 13:28:20

标签: ruby-on-rails ruby json postgresql ruby-on-rails-4

我有一个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">&times;</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 [2] ['text1']中替换部分视图@ deal.deal_info [parameter_i_want_to_pass] ['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">&times;</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'

2 个答案:

答案 0 :(得分:1)

您的变量名称有点令人困惑,但我认为这是您尝试做的事情。我使用each_with_index方法循环遍历deal deal_info内的每个模态。然后我使用locals: { }的{​​{1}}参数将变量传递给partial。部分然后引用这些变量,就像它们在本地定义一样。部分甚至不会真正需要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 %>

答案 1 :(得分:0)

deal1.deal_info = [ { "modal_id": "4", "text1":"lorem" }, 
      { "modal_id": "6", "video2":"yonak" },
      { "modal_id": "9", "video2":"boom" } ]

不是有效的ruby代码。您需要解析json字符串:

require 'json'

deal1.deal_info = JSON.parse('[
  { "modal_id": "4", "text1":"lorem" },
  { "modal_id": "6", "video2":"yonak" },
  { "modal_id": "9", "video2":"boom" }
]')

这会将其转换为哈希数组:

[{"modal_id"=>"4", "text1"=>"lorem"}, {"modal_id"=>"6", "video2"=>"yonak"}, {"modal_id"=>"9", "video2"=>"boom"}]