应用程序具有:客户端,它拥有并属于许多ActionItems,它拥有并属于许多客户端。用户选择客户端(他们作为客户的客户端),并向该客户端添加操作项(做)。 - 喜欢:用户创建=> "关于X主题的电子邮件客户端,"客户:Crayola LLC。
我已被指示在路线中嵌套资源:
resources :clients do
resources :action_items
end
这样我就可以获得如下的网址:
-http://localhost:3000/clients/42/action_items/11
显示特定客户的操作项。
然而 - 删除该客户的行动项目并不起作用。它一直试图将我重定向到我获得的销毁行动:
undefined local variable or method `clients_action_items' for # <ActionItemsController:0x007febd0edf800>
在此之前,使用销毁操作的删除链接试图将我重定向到我得到的节目页面:
No route matches [POST] "/clients/42/action_items/1"
然后我添加了:post&#39; / clients /:client_id / action_items /:id&#39; =&GT; &#39; action_items#摧毁&#39;到路线文件。 (现在我得到了未定义的局部变量或方法clients_action_items&#39;错误。
路线:
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>
我在迁移文件中创建了一个外键列,其中包含一个名为: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
- 新到铁轨。请原谅脏代码。这有什么不对?为什么销毁链接问题?为什么destroy链接重定向到之前显示,并给我路由和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
答案 0 :(得分:0)
您似乎正在尝试使用POST请求销毁对象。
No route matches [POST] "/clients/42/action_items/1"
您是否尝试过DELETE请求?
delete '/clients/:client_id/action_items/:id' => 'action_items#destroy'
您可以进一步了解CRUD(创建,读取,更新,删除)操作here
另外,你可以运行rake:routes并在此处发布输出吗?这将有助于确定实际生成的路线。
<强> 修改 强> 所以,正如你从rake的输出中看到的那样:路由有很多重复。你基本上有三个模型,User,Client和ActionItems,基本的CRUD和一个登录。
Rake文件:
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
我试图在这里简化结构。如果要执行从所有客户端的索引/列表页面删除客户端的所有操作项等任务,可以在ActionItems控制器中定义方法destroy_many,该方法将client_id作为参数,查询所有操作项并删除它们。 您不需要单独的ClientActionItem控制器/路由。 此外,如果要继续使用嵌套路由, 试试
<%= button_to('Delete Action Item', client_action_item_path(@client.id, list.id), method: :delete) %></br>
嵌套路由需要两个参数。第一个是client.id,第二个是action_item id。
答案 1 :(得分:0)
这应该可行,但未经测试。我不确定你的意图,但我会亲自使用link_to和一类按钮。
<%= button_to('Delete Action Item', client_action_item_path(@client, list), method: :delete) %></br>