0

以下は、複数のレコードを削除したいがエラーが発生するコードです... um_org_dataHTMLファイル名からのコードが含まれているという名前のビュー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>
4

2 に答える 2

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 %>
于 2013-08-15T10:48:41.893 に答える
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)
于 2013-08-15T10:35:05.400 に答える