Rails遍历参数数据

时间:2018-10-30 13:10:21

标签: ruby-on-rails ruby-on-rails-5

我正在解析一个excel文件并将数据传递给控制器​​,但在我看来似乎无法遍历它:

参数:

Parameters: {"data"=>{"consult_charges"=>[{"id"=>"17474", "item"=>"Consultation", "name"=>"Ramon", "price"=>"25.0"}, {"id"=>"17584", "item"=>"Consultation", "name"=>"Ramon", "price"=>"25.0"}, {"id"=>"17490", "item"=>"Consultation", "name"=>"Elizabeth", "price"=>"25.0"}, {"id"=>"17515", "item"=>"Consultation", "name"=>"Elizabeth", "price"=>"25.0"}, {"id"=>"17554", "item"=>"Consultation", "name"=>" Elizabeth", "price"=>"25.0"}, {"id"=>"17623", "item"=>"Consult - Referral Card", "name"=>"Elizabeth", "price"=>"0.0"}, {"id"=>"17486", "item"=>"Consultation", "name"=>"Racha", "price"=>"25.0"}

控制器:

@consult_charges = params["data"]["consult_charges"]

查看:

<table class="table awaken">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Item</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <% @consult_charges.each do |sale| %>
            <td><%= sale["id"] %></td>
            <td><%= sale["name"] %></td>
            <td><%= sale["item"] %></td>
            <td><%= sale["price"] %></td>
        <% end -%>
    </tbody>
</table>

结果是一行数据,就好像只有一个sale。当我查看@consult_charges的值时,它是一个像这样的项目数组:

<ActionController::Parameters {"id"=>"17584", "item"=>"Consultation", "name"=>"Ramon", "price"=>"25.0"} permitted: false>

我需要做些什么来转换类型吗?

1 个答案:

答案 0 :(得分:1)

之所以只显示一行,是因为每次迭代都缺少<tr>标签。将您的视图更改为;

<table class="table awaken">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Item</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
      <% @consult_charges.each do |sale| %>
        <tr>
            <td><%= sale["id"] %></td>
            <td><%= sale["name"] %></td>
            <td><%= sale["item"] %></td>
            <td><%= sale["price"] %></td>
        </tr>
      <% end -%>
    </tbody>
</table>