我有两个模型和一个像这样的连接表......
create_table "groups", :force => true do |t|
t.string "name"
create_table "shots", :force => true do |t|
t.integer "comedian_id"
t.string "pic_file_name"
create_table "group_shots", :force => true do |t|
t.integer "shot_id"
t.integer "group_id"
我想创建一个链接,从我的group_shots表中删除一行。到目前为止,我有......
<td><%= link_to 'Remove', groupshot_path(@group, shot), :method => :destroy %></td>
class GroupshotsController < ApplicationController
def destroy
GroupShot.where(params[:shot_id], params[:group_id]).delete
end
end
现在我收到了错误......
Routing Error
uninitialized constant GroupshotController
Try running rake routes for more information on available routes.
以下是我的路线:
groupshots GET /groupshots(.:format) groupshots#index
POST /groupshots(.:format) groupshots#create
new_groupshot GET /groupshots/new(.:format) groupshots#new
edit_groupshot GET /groupshots/:id/edit(.:format) groupshots#edit
groupshot GET /groupshots/:id(.:format) groupshots#show
PUT /groupshots/:id(.:format) groupshots#update
DELETE /groupshots/:id(.:format) groupshots#destroy
/groupshots/:id.:id(.:format) groupshot#destroy
答案 0 :(得分:1)
使用
<td><%= link_to 'Remove', groupshot_path(@group, shot), :method => :delete %></td>
要销毁记录,方法名称应为delete
而不是destroy
。
您收到的错误为uninitialized constant GroupshotController
,因为您的控制器名称为GroupshotsController
(注意多个群组)。
您已在routes.rb
中错误地定义了路线,应将其映射到
groupshots#destroy
(注意复数群组)
而不是groupshot#destroy
修改强>
在routes.rb中,将路径从/groupshots/:id.:id
更改为
/groupshots/:group_id/:shot_id
当你在销毁行动中访问params[:shot_id] and params[:group_id]
时。
答案 1 :(得分:0)
在 routes.rb
中添加以下行resources :groupshots #plural
如果您使用下面给出的链接删除它们,
<td><%= link_to 'Remove', groupshot_path(@group, shot), :method => :delete %></td>
它应该工作,似乎你的控制器名称是正确的。您错误地映射了路线。
您还可以在路线中单独映射删除操作。