删除方法在Rails中不起作用

时间:2012-09-26 16:37:43

标签: jquery ruby-on-rails ruby-on-rails-3 actioncontroller

我目前的代码如下:

 <%= javascript_include_tag "hchq.js", "bootstrap-image-gallery.js", "bootstrap-image-gallery.min.js", "load-image.min.js", "media.js", "jquery", "bootstrap.js", "jquery.dataTables.js", "DT_bootstrap.js" %>
  <%= javascript_include_tag :defaults %>

然而,我一直在:

ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"):
  actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.8) lib/rails/engine.rb:479:in `call'
  railties (3.2.8) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/local/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

我发现更多问题:Rails 3.1rc4 defaults.js not found建议我采用默认线路。我把它拿出来然后404就消失了,但是随着它的出现我不能发出删除请求。它只是引导我进入节目页面。

关于发生了什么以及如何解决这个问题的任何想法,所以我没有得到404并且可以发出删除请求?

更新

以下是实际删除链接:<%= link_to 'Delete', client_path(client), method: "delete", class: "label label-important" %>,它会生成以下html:<a href="/clients/2" class="label label-important" data-method="delete" rel="nofollow">Delete</a>,这似乎对我来说。

,这是日志中的一行:

Started GET "/clients/2" for 127.0.0.1 at 2012-09-27 16:50:23 -0500
Processing by ClientsController#show as HTML
  Parameters: {"id"=>"2"}
  Client Load (0.3ms)  SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT 1  [["id", "2"]]
  Rendered clients/show.html.erb within layouts/application (1.2ms)
Completed 200 OK in 47ms (Views: 45.4ms | ActiveRecord: 0.3ms)


Started GET "/assets/defaults.js" for 127.0.0.1 at 2012-09-27 16:50:23 -0500
Served asset /defaults.js - 404 Not Found (4ms)

我尝试的另一件事就是摆脱默认行,因为这是pre-rails 3.1,并且使用“application”作为javascript_include_tag内的唯一内容,我仍然有这个问题。

3 个答案:

答案 0 :(得分:2)

将Link_to更改为Button_to修复它。

为什么?

答案 1 :(得分:2)

唯一合理的解释与CRSF令牌Rails表单有关,当非GET操作即将到达服务器时。在rails中构建表单时,为了安全起见,您将获得一个传递给服务器的令牌。当您使用form_forform_tag帮助程序(表单使用POSTS和东西)时,这会自动内置。

由于protect_from_forgery中的application_controller,rails不允许您从表单外部执行POST / DELETE / PUT,这就是link_to不起作用的原因,它只是一个链接,试图做一个POST。但是如果你评论这一行,链接就可以了。

此处有更多信息:http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf

答案 2 :(得分:1)

您的删除链接只是一个链接,js magic将其转换为随后发布的表单。这就是为什么表单有效,链接没有 - 它必须只是保持一个链接。

至于为什么删除链接不起作用,你缺少js文件(因此404),或者如果你得到js include right,js中有错误,链接将被帮助器转换。在浏览器中检查您的js控制台并查找错误。

您的链接正在向您发送获取请求,因此很明显内置的rails js无法转换链接。