ruby on rails link_to delete方法不起作用

fxu*_*ser 9 forms ruby-on-rails link-to

我想使用以下代码删除帖子:

<%= link_to 'Destroy', post, :method => :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
Run Code Online (Sandbox Code Playgroud)

这不起作用......它只是将我重新定向回帖子 (posts/:id}

但是,如果我使用以下代码,它的工作原理

<%= button_to 'Destroy', post, method: :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
Run Code Online (Sandbox Code Playgroud)

是否可以link_tobutton_to这种情况一样表现?

编辑:破坏控制器功能

  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
Run Code Online (Sandbox Code Playgroud)

单击"销毁"按钮时记录:

Started GET "/posts/14" for 127.0.0.1 at 2012-10-21 15:38:28 +0300
Processing by PostsController#show as HTML
  Parameters: {"id"=>"14"}
  Post Load (0.4ms)  SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 14 LIMIT 1
  Rendered posts/show.html.erb within layouts/application (0.6ms)
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Completed 200 OK in 7ms (Views: 5.4ms | ActiveRecord: 0.8ms)
[2012-10-21 15:38:28] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Run Code Online (Sandbox Code Playgroud)

路线:

  devise_for :users

  resources :users

  resources :posts

  match '/about' => 'about#index'

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  root :to => 'index#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
  match '*a', :to => 'error#routing'
Run Code Online (Sandbox Code Playgroud)

har*_*h4u 20

你必须添加

//= require jquery
//= require jquery_ujs
Run Code Online (Sandbox Code Playgroud)

在javascripts/application.js文件中

第二,检查布局文件,

javascript_include_tag "application"
Run Code Online (Sandbox Code Playgroud)

包含与否?

希望这有帮助.


fxu*_*ser 11

解:

我在创建项目时从application.js中注释掉了这个

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
Run Code Online (Sandbox Code Playgroud)

所以重新添加它解决了问题

  • 今天添加回 jquery_ujs 为我解决了完全相同的问题。7年后,仍然是正确答案。根据文档,jquery_ujs 的功能之一是它允许您“从超链接发出非 GET 请求”。如果没有这个,DELETE 就会变成 GET。 (3认同)
  • 需要明确的是,“require jquery_ujs”似乎是重要的部分,以防不明显。 (2认同)

小智 6

首先尝试检查日志中生成的 HTTP post 方法。如果它是“Get”,则无论路由等如何,它都不会触发控制器中的删除操作。它必须是 HTTP 删除方法。

在最新版本的Rails(包括rails 6+)中,包含jquery UJS等不是一个选项/简单,您应该使用button_to helper而不是link_to。它创建一个删除 HTTP 帖子。

https://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to


Nan*_*ego 5

您是否在布局文件中包含了这一行?

<%= javascript_include_tag "application" %>
Run Code Online (Sandbox Code Playgroud)