代码之家  ›  专栏  ›  技术社区  ›  Jeremy Thomas

通过params数据循环的Rails

  •  0
  • Jeremy Thomas  · 技术社区  · 7 年前

    我正在分析一个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 回复  |  直到 7 年前
        1
  •  1
  •   kiddorails    7 年前

    它只显示一行的原因是 <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>