link_to 删除 url 不起作用

发布于 2024-10-07 03:04:23 字数 243 浏览 3 评论 0原文

我的应用程序中有以下 link_to 删除网址,

<%=link_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  

它似乎不起作用。当我单击此网址时,它只会将我带到显示路径。有人可以告诉我如何解决此问题吗?谢谢。

I have the following link_to delete url in my app

<%=link_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  

It does not seem to be working.When I click this url, it just takes me to the show path.Can someone please tell me how to fix this. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

还不是爱你 2024-10-14 03:04:23

你在使用 jQuery 吗?如果是这样,我认为问题可能是您使用的 jQuery 没有更新的rails.js 文件。

在这里下载rails.js:
https://github.com/rails/jquery-ujs/raw /master/src/rails.js
将其放入您的 javascripts 目录中,覆盖rails 默认附带的rails.js。

添加 javascript include 行以包含它。

  <%= javascript_include_tag "rails" %>

将其放在 Jquery include 标记之后。如果您不打算使用原型,您可能还想排除 javascript 默认值。

我在我的应用程序中包含了 jQuery UI,我发现删除现在可以正常工作,但在完成上述已解决的问题之后。

Are you using jQuery? If so, I think the problem could be that you are using jQuery without the updated rails.js file.

Download rails.js here:
https://github.com/rails/jquery-ujs/raw/master/src/rails.js
Drop it in your javascripts directory, overwriting the rails.js that comes default with rails.

Add a javascript include line to include it.

  <%= javascript_include_tag "rails" %>

Put this after your Jquery include tag. You probably also want to disinclude the javascript defaults if you don't plan on using prototype.

I included jQuery UI in my application, I found that delete is now working as show, but after doing above Resolved Issue.

哆啦不做梦 2024-10-14 03:04:23

确保这些行出现在 application.js 中:

 //= require jquery
 //= require jquery_ujs

Make sure these lines appear in application.js :

 //= require jquery
 //= require jquery_ujs
流云如水 2024-10-14 03:04:23

确保您已打开 Java 脚本。否则 :method => :delete 的作用与 Rails 中所示相同。

Ensure that you have java script turned on. Otherwise :method => :delete acts just as show in Rails.

一身骄傲 2024-10-14 03:04:23

如果您对博客使用静态路由,则以下内容应该有效:

<%= link_to "Delete", @blog, :method => :delete, :confirm => "Are you sure ?"%>

If you're using restful routing for blogs, then the following should work:

<%= link_to "Delete", @blog, :method => :delete, :confirm => "Are you sure ?"%>
我不咬妳我踢妳 2024-10-14 03:04:23

您可以尝试使用“data-method”而不是:method。

<%=link_to "Delete",blog_path(@blog.id), 'data-method' => :delete, :class => "delete", :confirm => "Are you sure ?"%> 

您可以在 jquery_ujs.js 上检查以下代码:

// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>

You can try with 'data-method' instead of :method.

<%=link_to "Delete",blog_path(@blog.id), 'data-method' => :delete, :class => "delete", :confirm => "Are you sure ?"%> 

You can check on jquery_ujs.js the following piece of code:

// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
泅人 2024-10-14 03:04:23

为了使 link_to 能够使用 delete 方法,Rails 需要 用于 jQuery 的不显眼的脚本适配器

  • 确保您的Gemfile具有

    gem 'jquery-rails'

  • Make确保 app/assets/javascripts/application.js

    //=需要jquery
    //= require jquery_ujs

  • 确保您的 app/views/layouts/application.html.erb

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' =>;正确%>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' =>;正确%>

    head 标记内。删除 'data-turbolinks-track' => true 部分(如果您不打算使用 Turbolinks)。

In order for link_to to work with the delete method, Rails needs the unobtrusive scripting adapter for jQuery.

  • Make sure that your Gemfile has

    gem 'jquery-rails'

  • Make sure that app/assets/javascripts/application.js has

    //= require jquery
    //= require jquery_ujs

  • Make sure that your app/views/layouts/application.html.erb has

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

    inside the head tag. Remove the 'data-turbolinks-track' => true section if you don't plan to use Turbolinks.

可是我不能没有你 2024-10-14 03:04:23

你应该使用

<%=button_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  

you should use

<%=button_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  
何处潇湘 2024-10-14 03:04:23

没有 jQuery 也可以有一个有效的 link_to

我已经找到了在没有 jQuery 的情况下为 Ruby on Rails 创建有效删除链接的最佳流程!在这里我已经做出了一个答案: https://stackoverflow.com/a/67710994/14387700

但是为了让这变得简单,我又在这里写下了。

我们需要处理 3 件事:

  1. articles_controller.rb 中添加 destroy 方法
  2. routes.rb 设置
  3. LINK_TO 标签

让我们开始...

添加 destroy 方法在articles_controller.rb中

首先我们会在articles_controller.rb中添加def destroy ... end
让我们打开:

# app/controllers/articles_controller.rb

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    params[:id] = nil
    flash[:notice] = "Art has been deleted"
    redirect_to :action => :index
  end

  1. 第一行中,我们调用变量“@article”,它将从我们的数据库中查找并选择 rails console 上文章的 ID 参数。然后,
  2. 在第二行中,@article 变量将在控制台中执行 destroy 命令。然后,
  3. 在第 3 行:id 参数将被删除,
  4. 在第 4 行,应用程序页面中将闪烁一条通知“艺术已被删除”,并且还将在控制台中显示,在数据库中找不到任何内容。
  5. 第5行,销毁过程完成后,我们将被重定向到文章索引页面。

这是发出破坏命令的主要因素。并使 link_to 正常工作。

设置routes.rb

但是等等
我们需要 2 个销毁页面的路由,它们是:

  1. GET 协议设置
  2. DELETE 协议设置

在路由中只需添加:

  resources :articles, except: [:destroy] # this will add all get request links automatically except destroy link

  post '/articles/new' => 'articles#create'
  post '/articles/:id' => 'articles#update'
  post '/articles/:id/edit' => 'articles#update' # this 3 lines are needed for other forms purpose

  # we need this 2 lines for our delete link_to setup
  delete 'articles/:id/delete' => 'articles#destroy', as: 'articles_delete'
  get '/articles/:id/delete' => 'articles#destroy'
  

这里

  1. 最后 2 行声明 DELETE 方法,

    • 'articles/:id/delete' 将是每个帖子的帖子链接标记(在 HTML 中称为:锚标记)中的链接结构,
    • '=>' 将链接结构指向控制器标记,即'articles#destroy'
    • 然后我们通过将 ** 设置为:'articles_delete'** 来定义路径文本,我们将使用它:
      link_to 标记中的“articles_delete_path”或“articles_delete_url”。

然后

  1. 在最后一行,我们定义了删除链接的获取请求,这将为我们提供一个类似于“https://2haas.com/articles/1/delete”的工作链接,但“/articles/1/destroy”除外,这意味着我们可以使用更多附加信息从这 2 种方法设置中自定义我们的删除链接。

最后最甜蜜的删除输出

所需的 link_to 标记

我们可以使用它来获取正确的删除 link_to 标记,该标记将起作用。

<%= link_to 'Delete Article', articles_delete_path, method: :delete %>
<%= link_to 'Delete Article', articles_delete_url, method: :delete %>

<% obj.each do |post| %>
<%= link_to 'Delete Article', articles_delete_path(post), method: :delete %>
<% end %>

除了 jQuery 之外,一切都已完成

感谢您正确阅读此答案。!

快乐编程

It's possible to have a working link_to without jQuery

I've found the best process to make a working delete link for ruby on rails without jQuery! Here I already make an answer: https://stackoverflow.com/a/67710994/14387700

But for making this easy, I'm writing this here again.

We need to work with 3 things:

  1. Adding destroy method in articles_controller.rb
  2. routes.rb setup
  3. LINK_TO tag

Let's Start...

Adding destroy method in articles_controller.rb

At first, we will add def destroy ... end in articles_controller.rb,
lets open:

# app/controllers/articles_controller.rb

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    params[:id] = nil
    flash[:notice] = "Art has been deleted"
    redirect_to :action => :index
  end

Here

  1. in the 1st line, we're calling a variable '@article' which will find and select the ID parameters of the article on rails console from our database. Then,
  2. in 2nd line, the @article variable will make the destroy command in the console. then,
  3. in 3rd line: the id params will be deleted and
  4. in 4th line, a notice will flash in application page "Art has been deleted" and also will show in console that, found nothing in the database.
  5. In 5th line, after the destroying process completed, we will be redirected to the article index page.

This is the main factor which will give the destroying command. and make the link_to working.

Setup routes.rb

BUT WAIT
We need 2 routes for the destroy page which are:

  1. A GET protocol setup
  2. A DELETE protocol setup

In routes just add:

  resources :articles, except: [:destroy] # this will add all get request links automatically except destroy link

  post '/articles/new' => 'articles#create'
  post '/articles/:id' => 'articles#update'
  post '/articles/:id/edit' => 'articles#update' # this 3 lines are needed for other forms purpose

  # we need this 2 lines for our delete link_to setup
  delete 'articles/:id/delete' => 'articles#destroy', as: 'articles_delete'
  get '/articles/:id/delete' => 'articles#destroy'
  

Here

  1. The 2nd last line is declaring the DELETE method,

    • 'articles/:id/delete' will be the link structure in post link tag (known as: anchor tag in HTML) for every single post,
    • '=>' is pointing the link structure to the controller tag which is 'articles#destroy',
    • then we defined the path text by setting ** as: 'articles_delete'** which we will use as:
      'articles_delete_path' or 'articles_delete_url' in link_to tag.

Then

  1. in last line, we defined the get request for the delete link which will give us a working link like "https://2haas.com/articles/1/delete" except "/articles/1/destroy" that means we can customize our delete link from this 2 methods setup with more additional information..

Last sweetest delete output

The desired link_to tag

we can use this to get proper delete link_to tag which will work.

<%= link_to 'Delete Article', articles_delete_path, method: :delete %>
<%= link_to 'Delete Article', articles_delete_url, method: :delete %>

<% obj.each do |post| %>
<%= link_to 'Delete Article', articles_delete_path(post), method: :delete %>
<% end %>

AND that's done except jQuery

Thanks for reading this answer properly.!

HAPPY PROGRAMMINGS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文