删除数据库ROR中的多个记录时出错

时间:2013-08-15 10:13:53

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

以下是我想删除多条记录但收到错误的代码... 我的视图名为um_org_data,其中html文件名的代码为index.html.erb

<%= form_tag destroy_um_org_data_path, method: :delete,  data: { confirm: 'Are you sure you want to delete all these?' } do %>
        <table class="table table-hover table-condensed">
          <tr>
            <th></th>
            <th>Organization Name</th>
            <th>Organization Description</th>
            <th>Office Address</th>
            <th>Office Phone Number</th>
            <th>Actions</th>
          </tr>


         <% @um_org_data.each do |um_org_data| %>
            <tr>
              <td><%= check_box_tag "deleted_ids[]", um_org_data.id %></td>
              <td><%= um_org_data.org_name %></td>
              <td><%= um_org_data.org_description%></td>
              <td><%= um_org_data.offc_addr%></td>
              <td><%= um_org_data.offc_ph%></td>
              <td>
                <%= link_to "<i class='icon-eye-open'></i>".html_safe, um_org_data,"data-original-title" => "View Details", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>
                <%= link_to "<i class= 'icon-edit'></i>".html_safe, edit_um_org_datum_path(um_org_data), "data-original-title" => "Edit", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none'%>

                <%= link_to "<i class= 'icon-trash'></i>".html_safe, um_org_data, method: :delete, data: { confirm: 'Are you sure?' }, "data-original-title" => "Delete", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>



              </td>
          </tr>

        <%end%>
        </table>
        <%= button_tag(type: "submit", class: "btn") do %>
           Delete Selected <i class="icon-trash"></i>
        <% end %>
        <% end %>

为此我的控制器名称um_org_data_controller.rb 有针对此操作的以下方法。

def destroy
    @um_org_datum = UmOrgDatum.find(params[:id])
    @um_org_datum.destroy

    respond_to do |format|
      format.html { redirect_to um_org_data_url }
      format.json { head :no_content }
    end
  end

我在视图名称index.html.erb

的第一行遇到错误

以下是我发现的错误细节。

NameError in Um_org_data#index

undefined local variable or method `destroy_um_org_data_path' for #<#<Class:0x00000004582a10>:0x007fa9e42f4e38>

 <%= form_tag destroy_um_org_data_path, method: :delete,  data: { confirm: 'Are you sure you want to delete all these?' } do %>
        <table class="table table-hover table-condensed">
           <tr>
             <th></th>

如果在视图代码中删除以下工作正常...... 在此先感谢。

现在改变了我的代码.....

<%= form_tag url: destroy_selected_um_org_data_path, method: :delete,  data: { confirm: 'Are you sure you want to delete all these?' } do %>
        <table class="table table-hover table-condensed">
          <tr>
            <th></th>
            <th>Organization Name</th>
            <th>Organization Description</th>
            <th>Office Address</th>
            <th>Office Phone Number</th>
            <th>Actions</th>
          </tr>


         <% @um_org_data.each do |um_org_data| %>
            <tr>
              <td><%= check_box_tag "um_org_data_ids[]", um_org_data.id %></td>
              <td><%= um_org_data.org_name %></td>
              <td><%= um_org_data.org_description%></td>
              <td><%= um_org_data.offc_addr%></td>
              <td><%= um_org_data.offc_ph%></td>
              <td>
                <%= link_to "<i class='icon-eye-open'></i>".html_safe, um_org_data,"data-original-title" => "View Details", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>
                <%= link_to "<i class= 'icon-edit'></i>".html_safe, edit_um_org_datum_path(um_org_data), "data-original-title" => "Edit", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none'%>

                <%= link_to "<i class= 'icon-trash'></i>".html_safe, um_org_data, method: :delete, data: { confirm: 'Are you sure?' }, "data-original-title" => "Delete", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>



              </td>
          </tr>

        <%end%>
        </table>
        <%= button_tag(type: "submit", class: "btn") do %>
           Delete Selected <i class="icon-trash"></i>
        <% end %>
        <% end %>

控制器代码如下:

def destroy_selected
    @um_org_data = UmOrgData.find(params[:um_org_data_ids])
    @um_org_data.each { |o| o.destroy }

    respond_to do |format|
      format.html { redirect_to um_org_data_url }
      format.json { head :no_content }
    end

  end

,错误如下:

 NameError in Um_org_data#index

undefined local variable or method `destroy_selected_um_org_data_path' for #<#<Class:0x00000004663bf0>:0x0000000466d3a8>

<%= form_tag url: destroy_selected_um_org_data_path, method: :delete,  data: { confirm: 'Are you sure you want to delete all these?' } do %>
        <table class="table table-hover table-condensed">
           <tr>
            <th></th>

2 个答案:

答案 0 :(得分:1)

由资源路由生成的删除辅助方法与仅show method: :delete操作的辅助方法基本相同,因此没有destroy_*辅助方法。

即,您将使用:link_to um_org_data_path(um_org_data), method: :delete

但是你想要实现的目标并不是这样。由于destroy方法仅适用于单个对象(它需要ID),而不适用于对象集合。所以我猜你需要这样的东西:

# routes.rb
resources :um_org_data do
  collection do
    delete :destroy_selected
  end
end

# um_org_data_controller.rb
class UmOrgDataController < AC::Base
  def destroy_selected
    @um_org_data = UmOrgData.find(params[:um_org_data_ids])
    @um_org_data.each { |o| o.destroy }
    # ...
  end
end

# index.html.erb
<%= form_tag url: destroy_selected_um_org_data_path, method: :delete do %>
  <% @um_org_data.each do |um_org_data| %>
    <tr>
      <td><%= check_box_tag "um_org_data_ids[]", um_org_data.id %></td>
    # ...
    </tr>
  <% end %>
<% end %>

答案 1 :(得分:0)

以下link_to中的路径不正确:

<%= link_to "<i class= 'icon-trash'></i>".html_safe, um_org_data, method: :delete, data: { confirm: 'Are you sure?' }, "data-original-title" => "Delete", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>

尝试替换:

um_org_data

通过

um_org_data_path(um_org_data)