简体   繁体   English

Rails遍历参数数据

[英]Rails looping through params data

I am parsing an excel file and passing the data to my controller but I cannot seem to loop through it in my view: 我正在解析一个excel文件并将数据传递给控制器​​,但在我看来似乎无法遍历它:

Params: PARAMS:

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"}

Controller: 控制器:

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

View: 视图:

<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>

The result is a single line of data as if there is only one sale . 结果是单行数据,好像只有一次sale When I look at the value for @consult_charges it's an array of items like this: 当我查看@consult_charges的值时,它是一个像这样的项目数组:

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

Do I need to do something to convert the type? 我需要做些什么来转换类型吗?

The reason it's showing just one line is because <tr> tag is missing on each iteration. 之所以只显示一行,是因为每次迭代都缺少<tr>标记。 Change your view to; 将您的视图更改为;

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM