【问题标题】:Passing parameter to partial view - Rails 4/postgresql/json将参数传递给局部视图 - Rails 4/postgresql/json
【发布时间】: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">&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[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">&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'

【问题讨论】:

  • 上述错误信息指的是哪个文件中的哪一行?我在您发布的代码中看不到您使用 modal_number 的位置。

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


【解决方案1】:
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"}] 

【讨论】:

    【解决方案2】:

    您的变量名称有点令人困惑,但我认为这就是您想要做的。我正在使用each_with_index 方法循环遍历dealdeal_info 内部的每个模态。然后我为render partial: 使用locals: { } 参数,以便将变量传递给部分。部分然后引用这些变量,就像它们在本地定义一样。部分甚至最终都不需要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">&times;</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>
    ...
    

    【讨论】:

    • 感谢您的帮助,现在将检查它。看起来非常聪明和分解/干净:)
    猜你喜欢
    • 2011-10-04
    • 2014-01-14
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多