我不能自定义操作应对在任务控制器我有行动:在控制器
def destroy_all
current_user.tasks.destroy
redirect_to root_path
end
我的数据库架构看起来象下面这样:
create_table "tasks", force: true do |t|
t.string "content"
t.boolean "done"
t.datetime "created_at"
t.datetime "updated_at"
t.string "users_id"
end
和我有关系:任务belongs_to的用户和用户has_many任务。 通过操作destory_all
我想销毁所有当前用户的任务。
但是当我点击:
<%= link_to "delete all", { controller: 'tasks', action: 'destroy_all'}, method: 'delete' %>
没有发生。用户仍然有他所有的任务。从服务器控制台
登录:
Started DELETE "/tasks/destroy_all" for 127.0.0.1 at 2014-10-04 23:29:27 +0200
Processing by TasksController#destroy_all as HTML
Parameters: {"authenticity_token"=>"fVPDi6bczNXlfhjCDI4pJhMUm3cjv6TN1Ny/ulUO4YQ="}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 31ms (ActiveRecord: 0.2ms)
Started GET "/" for 127.0.0.1 at 2014-10-04 23:29:27 +0200
Processing by PagesController#home as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
Task Load (0.1ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."done" = 'f' AND "tasks"."user_id" = 2
Rendered tasks/index.html.erb within layouts/application (2.4ms)
Rendered layouts/_userbar.html.erb (0.2ms)
Rendered layouts/_menu.html.erb (0.1ms)
Completed 200 OK in 46ms (Views: 44.3ms | ActiveRecord: 0.3ms)
而且的routes.rb文件
resources :tasks do
collection do
delete :destroy_all
end
end
编辑:我更新的日志和的link_to方法和我说的routes.rb文件。
您是否在日志中看到任何错误或警告? – 2014-10-04 15:55:24
没有错误,我可以识别。我添加了日志片段。 – Jensky 2014-10-04 16:00:11