在rails 4中调用destroy会在#update中抛出错误。调用时,更新代码可以正常工作,new / create也是如此。
路线:
namespace :admin do
resources :pages
end
match ':controller(/:action(/:id))', :via => [:get, :post]
控制器:
class Admin::PagesController < ApplicationController
layout 'admin'
...
# PUT /pages/1
# PUT /pages/1.xml
def update
logger.info("Starting update")
@page = Page.find(params[:id])
if @page.update_attributes(page_params)
flash[:notice] = "Page updated successfully."
redirect_to(:action => 'show')
else
@page_count = Page.count
render('edit')
end
end
def delete
logger.info("Starting delete")
@page = Page.find(params[:id])
logger.info("Ending delete")
end
def destroy
logger.info("Starting destroy")
#page = Page.find(params[:id]).destroy
@page.destroy
logger.info("After destroy")
flash[:notice] = "Page destroyed successfully."
redirect_to(:action => 'index')
end
# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.require(:page).permit(:sport, :name, :html_code, :permalink)
end
调用delete html.erb
<% @page_title = "Delete Page" %>
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="pages delete">
<h2>Delete Page</h2>
<%= form_for([:admin, @page], :url => {:action => 'destroy', :id => @page.id}) do |f| %>
<p>Are you sure you want to permanently delete this page?</p>
<p class="reference-name"><%= @page.permalink %></p>
<div class="form-buttons">
<%= submit_tag("Delete Page") %>
</div>
<% end %>
</div>
控制台:
Started GET "/admin/pages/delete/4" for 127.0.0.1 at 2014-03-07 12:56:38 -0800
Processing by Admin::PagesController#delete as HTML
Parameters: {"id"=>"4"}
Starting delete
Page Load (0.3ms) SELECT `pages`.* FROM `pages` WHERE `pages`.`id` = 4 LIMIT 1
Ending delete
Rendered admin/pages/delete.html.erb within layouts/admin (1.1ms)
Completed 200 OK in 10ms (Views: 8.2ms | ActiveRecord: 0.3ms)
Started PATCH "/admin/pages/4" for 127.0.0.1 at 2014-03-07 12:56:46 -0800
Processing by Admin::PagesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3al7JBY9e1nl+xq7uH2m2S+sWDmhvFLhxuYeUp5ZKv8=", "commit"=>"Delete Page", "id"=>"4"}
Starting update
Page Load (0.3ms) SELECT `pages`.* FROM `pages` WHERE `pages`.`id` = 4 LIMIT 1
Completed 400 Bad Request in 2ms
ActionController::ParameterMissing (param not found: page):
app/controllers/admin/pages_controller.rb:85:in `page_params'
app/controllers/admin/pages_controller.rb:59:in `update'
Rendered /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
Rendered /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.8ms)
答案 0 :(得分:3)
首先,从config/routes.rb
match ':controller(/:action(/:id))', :via => [:get, :post]
这是一个包罗万象的路线,可能会导致您的应用程序出现不当行为。根据{{3}},您应该使用resources
和朋友。
其次,您的所有表单实际上只是一个删除按钮。您可以更轻松地完成此任务:
<p>Are you sure you want to permanently delete this page?</p>
<p class="reference-name"><%= @page.permalink %></p>
<%= button_to "Delete Page", [:admin, :page], :method => :delete %>
button_to
方法会创建一个表单,因此您不必这样做。这里需要注意的是button_to
正在使用:method => :delete
,这将导致DELETE /admin/pages/4
路由被使用,这一点非常重要。这将转到您控制器中的destroy
操作。
您的表单之前没有执行此操作,它正在向同一路由提交PUT
请求,该请求转到update
操作,这就是您所要求的看到发生了。