破坏行动表现异常

Destroy Action Acting Unusual

该应用程序有:客户端,它具有并属于许多 ActionItems,它具有并属于许多客户端。用户选择一个客户(他们作为客户拥有的客户),然后向该客户添加操作项(待办事项)。 -- 喜欢:用户为客户创建 => "Email client about X topic,":Crayola LLC。

我被指示像这样在路线中嵌套资源:

resources :clients do
resources :action_items
end 

这样我就可以得到 URL 比如:

-http://localhost:3000/clients/42/action_items/11

显示特定客户的操作项。

但是 - 删除该客户的操作项不起作用。它一直试图将我重定向到销毁操作,我得到:

undefined local variable or method `clients_action_items' for #    <ActionItemsController:0x007febd0edf800>

在此之前,使用销毁操作的删除 link 试图将我重定向到显示页面,我在该页面上得到:

No route matches [POST] "/clients/42/action_items/1"

然后我添加:post '/clients/:client_id/action_items/:id' => 'action_items#destroy' 到路由文件。 (现在我得到了未定义的局部变量或方法 clients_action_items' 错误。

路线:

Rails.application.routes.draw do


get 'users/index'

  get 'users/new'

  get 'users/edit'

  get 'users/delete'

  get 'users/create'

  patch 'users/create'

  patch 'users/update'

  get 'clients/index'

  get 'clients/new'

  get 'clients/edit'

  get 'clients/delete' => 'clients#delete'

  get 'clients/create'

  patch 'clients/create'

  patch 'clients/update'


 post '/clients/:client_id/action_items/:id' => 'action_items#destroy'



  get 'login', :to => "access#index"

    resources :action_items

  #/clients/13/action_items

  resources :clients do
    resources :action_items
  end

  #get 'home/index'

  #get 'home/edit'
#
  #get 'home/delete'

  #get 'home/show'

root 'home#index'

#define, below **, is the URL we named categories/index. It is now localhost:3000/define


  #get 'index' => 'questions#index'

  #get 'questions/edit'

  #get 'new' => 'questions#new'

  #get 'questions/delete'

  #post 'questions/destroy'

  #get 'questions/show'

  #post 'create' => 'questions#create'



  match ':controller(/:action(/:id))', :via => [:get, :post]

  #   end
end

行动项目控制器:

class ActionItemsController < ApplicationController

 # before_action :get_owner

    def index
        @action_items = ActionItem.all 
        @client = Client.find(params[:client_id])

    end 

    def new
        @action_items = ActionItem.new
        # @action_items_client = @client.action_items.new 
        @client = Client.find(params[:client_id])
    end

    def create
            # @action_item = ActionItem.new(action_items_params)
   #        if @action_item.save
   #                redirect_to(:action => 'show', :id => @action_item.id)
   #                #renders client individual page
   #        else 
   #                redirect_to(:action => 'new')
    #       end
    @client = Client.find(params[:client_id])

            @action_item_client = @client.action_items.new(action_items_params)
            if @action_item_client.save
                redirect_to(:action => 'show', :id => @action_item_client.id, :client_id => @client.id)
            else 
                redirect_to(:action => 'new')
            end
    end

    def edit
        @action_item = ActionItem.find(params[:id])
    end

    def update
        @action_item = ActionItem.find(params[:id])
        if @action_item.update_attributes(action_items_params)
            redirect_to(:controller => 'action_items', :action => 'show', :id => @action_item.id)
            flash[:notice] = "Updated"
            else 
                render 'new'

        end 
    end


    def show
        @client = Client.find(params[:id])  
        @action_item = ActionItem.find(params[:action_item_id])


    end

    def action_clients
        @action_clients = ActionItem.Client.new 
    end



    def delete
        @action_item = @client.action_items.find(params[:client_id])
    end

    def destroy
        # @action_items = @client.action_items.find(params[:id]).destroy
        # redirect_to(:controller => 'action_items', :action => 'index')

        item = clients_action_items.find(params[:client_id])
        item.destroy
        if params[:client_id]
            redirect_to clients_action_items_path(params[:client_id])
        else 
            redirect_to clients_action_items_path
        end 
    end

    private 
    def action_items_params
        params.require(:action_item).permit(:purpose, :correspondence_method, :know_person, :contact_name_answer, :additional_notes)
    end

    # private
    # def get_owner
    #   if params[:client_id].present?
    #       @owner = user.clients.find(params[:client_id])
    #   else
    #       @owner = user
    #   end 
    # end
  end

我要从中删除操作项的索引视图:

<%= link_to('New Action Item', :controller => 'action_items', :action => 'new') %></br>

<ol><% @action_items.each do |list| %>

<li>

Action Item for <%= @client.name %> is: <strong><%= list.correspondence_method %></strong> Client, about:

<strong><%= list.purpose %> </strong></li>

And you created some additional notes: <strong><%= list.additional_notes %></strong></br></br>  



-- Crud Actions -- </br>

<%= link_to('New Action Item', :controller => 'action_items', :action => 'new') %></br>
<%= link_to('Edit Action Item', :controller => 'action_items', :action => 'edit', :id => list.id) %></br>
<%= link_to('Show Individual', :controller => 'action_items', :action => 'show', :id => list.id) %></br>
<%= button_to('Delete Action Item', :controller => 'action_items', :action => 'destroy', :id => list.id) %></br> 

<h2> new delete </h2> 

</br></br>

<% end %></ol>

我在一个迁移文件中创建了外键列,其中包含一个名为 table 的连接:action_items_clients:

class CreateActionItemsClients < ActiveRecord::Migration
  def change
    create_table :action_items_clients, :id => false do |t|
        t.integer :action_item_id 
        t.integer :client_id
    end
  end
end

-rails 的新手。请原谅脏代码。这里有什么问题?为什么 destroy link 问题?为什么 destroy link 重定向到 show before,并给我路由和 ID 错误?

感谢您的宝贵时间。 *** 编辑 ****

Rake 路由输出:

               Prefix Verb     URI Pattern                                         Controller#Action
            users_index GET      /users/index(.:format)                              users#index
              users_new GET      /users/new(.:format)                                users#new
             users_edit GET      /users/edit(.:format)                               users#edit
           users_delete GET      /users/delete(.:format)                             users#delete
           users_create GET      /users/create(.:format)                             users#create
                        PATCH    /users/create(.:format)                             users#create
           users_update PATCH    /users/update(.:format)                             users#update
          clients_index GET      /clients/index(.:format)                            clients#index
            clients_new GET      /clients/new(.:format)                              clients#new
           clients_edit GET      /clients/edit(.:format)                             clients#edit
         clients_delete GET      /clients/delete(.:format)                           clients#delete
         clients_create GET      /clients/create(.:format)                           clients#create
                        PATCH    /clients/create(.:format)                           clients#create
         clients_update PATCH    /clients/update(.:format)                           clients#update
                        DELETE   /clients/:client_id/action_items/:id(.:format)      action_items#destroy
                  login GET      /login(.:format)                                    access#index
           action_items GET      /action_items(.:format)                             action_items#index
                        POST     /action_items(.:format)                             action_items#create
        new_action_item GET      /action_items/new(.:format)                         action_items#new
       edit_action_item GET      /action_items/:id/edit(.:format)                    action_items#edit
            action_item GET      /action_items/:id(.:format)                         action_items#show
                        PATCH    /action_items/:id(.:format)                         action_items#update
                        PUT      /action_items/:id(.:format)                         action_items#update
                        DELETE   /action_items/:id(.:format)                         action_items#destroy
    client_action_items GET      /clients/:client_id/action_items(.:format)          action_items#index
                        POST     /clients/:client_id/action_items(.:format)          action_items#create
 new_client_action_item GET      /clients/:client_id/action_items/new(.:format)      action_items#new
edit_client_action_item GET      /clients/:client_id/action_items/:id/edit(.:format) action_items#edit
     client_action_item GET      /clients/:client_id/action_items/:id(.:format)      action_items#show
                        PATCH    /clients/:client_id/action_items/:id(.:format)      action_items#update
                        PUT      /clients/:client_id/action_items/:id(.:format)      action_items#update
                        DELETE   /clients/:client_id/action_items/:id(.:format)      action_items#destroy
                clients GET      /clients(.:format)                                  clients#index
                        POST     /clients(.:format)                                  clients#create
             new_client GET      /clients/new(.:format)                              clients#new
            edit_client GET      /clients/:id/edit(.:format)                         clients#edit
                 client GET      /clients/:id(.:format)                              clients#show
                        PATCH    /clients/:id(.:format)                              clients#update
                        PUT      /clients/:id(.:format)                              clients#update
                        DELETE   /clients/:id(.:format)                              clients#destroy
                   root GET      /                                                   home#index
                        GET|POST /:controller(/:action(/:id))(.:format)              :controller#:action

您似乎正试图使用​​ POST 请求销毁对象。

No route matches [POST] "/clients/42/action_items/1"

您是否尝试过删除请求?

delete '/clients/:client_id/action_items/:id' => 'action_items#destroy'

您可以进一步了解 CRUD(创建、读取、更新、删除)操作 here

另外,你能运行 rake:routes 和 post 这里输出吗?这将有助于找出实际生成的路线。

编辑 因此,正如您从 rake:routes 的输出中看到的那样,有很多重复项。您基本上拥有三个模型,用户、客户端和具有基本 CRUD 和一次登录的 ActionItems。

耙子文件:

Rails.application.routes.draw do
  get 'login', :to => "access#index"
  resources :users
  resources :action_items
  resources :clients do
    member do
      get :action_items
    end
  end
  root 'home#index'
end

Client 和 ActionItems 具有多对多关系。如果它是一对多的,即许多 ActionItem 只属于一个 Client,那么嵌套资源会更好。

要显示客户端的所有操作项,您只需要客户端控制器中的一个额外方法。

客户端控制器:

def show
  @client = Client.find(params[:id])  
end

def action_items
  @list_action_items = @client.action_items
end

行动项目控制器:

#Will list all action_items irrespective of which clients they belong to
def index
  @action_items = ActionItem.all  
end 

#For creating a new action item, 
def new
  @action_item = ActionItem.new
  #You can render a form with dropdown here with Client.all to assign the new action_item to a client
end

def create
  @action_item = ActionItem.new(action_items_params)
  if @action_item.save
    redirect_to(:action => 'show', :id => @action_item.id)
    #renders client individual page
  else 
    redirect_to(:action => 'new')
  end
end

def edit
  @action_item = ActionItem.find(params[:id])
end

def update
  if @action_item.update_attributes(action_items_params)
    flash[:notice] = "Updated"
    redirect_to(:controller => 'action_items', :action => 'show', :id => @action_item.id)
  else 
    render 'new'
  end 
end


def show
  @action_item = ActionItem.find(params[:id])
end

def destroy
  @action_item.destroy
  flash[:notice] = "Action Item has been deleted."
  redirect_to action_items_path
end

这里我尽量简化了结构。如果你想执行从所有客户端的 index/list 页面删除一个客户端的所有操作项的任务,你可以在 ActionItems 控制器中定义一个方法 destroy_many 以 client_id 作为参数,查询所有操作项并删除它们。 您不需要单独的 ClientActionItem controller/routes。 另外,如果你想继续使用嵌套路由, 尝试

<%= button_to('Delete Action Item', client_action_item_path(@client.id, list.id), method: :delete) %></br>

嵌套路由需要两个参数。第一个是 client.id,第二个是 action_item id。

这应该可行,但未经测试。我不确定你的意图,但我个人会使用 link_to 和 class 按钮。

<%= button_to('Delete Action Item', client_action_item_path(@client, list), method: :delete) %></br>