0

在这段代码中,我为每个项目提供了复选框,使用户能够一次删除多个项目,但是如果用户在不检查任何项目的情况下按下删除按钮,则会出现错误:

<% form_for :product , :url => { :action => :delete_selected } 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 %>
    <%= submit_tag "Remove selected items" , :confirm => "Are you really want to delete these items ?" %>
</div>
<% end %>
  1. 我可以在发送到控制器之前检查这一点并向用户发出警报还是应该在控制器中完成?

  2. 如果我想添加另一种一次编辑多个项目的方法,是否可以在此表单中进行此操作?我的意思是这可能对一种形式有不同的操作?

4

1 回答 1

1

如果您至少使用 Rails 2.3,请查看嵌套表单。它包括一个帮助器,可将“删除”复选框添加到父表单中的关联对象。它还将为您提供编辑每个相关项目的表格。

http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

http://jimneath.org/2008/09/06/multi-model-forms-validations-in-ruby-on-rails/

于 2009-08-22T20:48:23.110 回答