如何在此视图中将多个ID发送到控制器?

时间:2009-08-19 23:04:07

标签: ruby-on-rails checkbox

我想在此代码中将带有复选框的多个ID发送到控制器:

<% form_for :product do %>
<table border="1px">
    <tr>
        <th>
            Select
        </th>
        <th>
            Image
        </th>
        <th>
            Product Name
        </th>
        <th>
            Product Description
        </th>
        <th>
            Product Price
        </th>
        <th>
            Categories
        </th>
        <th colspan="3">
            Actions
        </th>
    </tr>
    <% @products.each do |p| %>
    <tr>
        <td>
            <%= check_box_tag "product_ids[]", p.id, false, :id => "product_#{p.id}" %>
        </td>
        <td>
            <%= image_tag p.photo.url(:thumb) , :alt => "#{p.name}" %>
        </td>
        <td>
            <%= link_to "#{p.name}" , edit_product_path(p) %>
        </td>
        <td>
            <%=h truncate(p.description.gsub(/<.*?>/,''),:length => 80) %>
        </td>
        <td>
            <%=h p.price %>
        </td>
        <td>
            <% for category in p.categories.find(:all) %>
            <%= link_to "#{category.name}" , category_path(category.id) %>
            <% end %>
        </td>
        <td>
            <%= link_to 'Show' , product_path(p) %>
        </td>
        <td>
            <%= link_to 'Edit', edit_product_path(p) %>
        </td>
        <td>
            <%= link_to 'Remove', product_path(p), :confirm => "Are you really want to delete #{p.name} ?", :method => 'delete' %>
        </td>
        <% end %>
    </tr>
</table>
<div id="products_nav">
    <%= link_to "Add a new Product" , new_product_path %>
    <%= link_to "Add a new Category" , new_category_path %>
    <%= link_to "Category page" , categories_path %>
    <%= link_to "Remove selected products" , delete_selected_products_path , :method => 'delete' %>
</div>
<% end %>

代码在这一行:

<%= link_to "Remove selected products" , delete_selected_products_path , :method => 'delete' %>

1 个答案:

答案 0 :(得分:0)

乍一看它看起来不错,虽然“delete_selected_products_path”路线不典型。你有专门为此设置的路线吗?

在任何情况下,您都应该能够将此删除请求发送到常用方法,并让控制器检查参数。

Railscast可能会有所帮助。