Ruby-on-rails Rails 4 link_to Destroy 在入门教程中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18154916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:31:01  来源:igfitidea点击:

Rails 4 link_to Destroy not working in Getting Started tutorial

ruby-on-rails

提问by hyperview.ca

I am working through the Getting Started tutorial (creating a Blog) and the link_to Destroy is not functioning properly. In the terminal it always interprets it as #SHOW.

我正在学习入门教程(创建博客),但 link_to Destroy 无法正常运行。在终端中,它总是将其解释为#SHOW。

In reading similar issues I have learned that the Delete must be converted to a POST for the browser to interpret it. This doesn't seem to be happening.

在阅读类似问题时,我了解到必须将 Delete 转换为 POST 以便浏览器对其进行解释。这似乎不会发生。

I have run into the same problem using Destroy in the Lynda.com Rails course as well, so it leads me to believe it is something in my development environment. I am using Rails 4, Ruby 2.00p274, MySQL, WEBrick for the HTTP server on a MacBook Pro Lion.

我在 Lynda.com Rails 课程中使用 Destroy 也遇到了同样的问题,所以这让我相信这是我的开发环境中的问题。我在 MacBook Pro Lion 上使用 Rails 4、Ruby 2.00p274、MySQL、WEBrick 作为 HTTP 服务器。

in the terminal session when Destroy is selected:

在选择销毁时的终端会话中:

Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600
Processing by PostsController#show as HTML
  Parameters: {"id"=>"4"}
  Post Load (0.6ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", "4"]]
  Rendered posts/show.html.erb within layouts/application (0.4ms)
Completed 200 OK in 13ms (Views: 8.6ms | ActiveRecord: 0.6ms)

In the ports-controller.rb:

在端口控制器.rb 中:

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

    redirect_to action: :index
end

In the index.html.erb:

在 index.html.erb 中:

<% @posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
     <td><%= post.text %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
????<td><%= link_to 'Destroy',  { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
 <% end %>

In the routes.rb

在routes.rb

Blog::Application.routes.draw do
   resources :posts do
     resources :comments
   end
  root to: 'welcome#index'
end

回答by hawk

Try this

尝试这个

<%= link_to 'Destroy',  post,  method: :delete, data: { confirm: 'Are you sure?' } %>

回答by rlshep

Make sure gem 'jquery-rails' is in gemfile and jquery_ujs is included in app/assets/javascripts/application.js file

确保 gem 'jquery-rails' 在 gemfile 中并且 jquery_ujs 包含在 app/assets/javascripts/application.js 文件中

//= require jquery
//= require jquery_ujs

Source: https://github.com/rails/jquery-ujs

来源:https: //github.com/rails/jquery-ujs

回答by nowRails

I had the same issue, for rails 4.2.X. Checked all my javascript files but could not make it work. if u look closely at the server request u will be missing 'authenticity_token' in the params, so user gets logged out. In rails 4.1 and above u have to use button_toinstead of link_to

对于 rails 4.2.X,我遇到了同样的问题。检查了我所有的 javascript 文件,但无法使其工作。如果您仔细查看服务器请求,您将在参数中丢失“authenticity_token”,因此用户会被注销。在 Rails 4.1 及更高版本中,您必须使用button_to而不是 link_to

回答by Marcelo Bento

I solved the problem, just by adding in assets/javascripts/application.js

我解决了这个问题,只需添加 assets/javascripts/application.js

//= require jquery_ujs

回答by Douglas G. Allen

Make sure that you have a Delete REST method via rake routes.

确保你有一个通过 rake 路由的 Delete REST 方法。

DELETE /articles/:id(.:format)                     articles#destroy

I've got the same problem only with this blog version as I'm doing both. http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/

我只有这个博客版本有同样的问题,因为我正在做这两个。 http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/

I'm also trying to learn how to use the web console and pry within it. My problem is that binding.pry doesn't show up in destroy method but will in show. That tells me it must be a bad link right? It's not getting to the destroy method even. Thank you all for your answers. We do have to try things don't we? Trying

我也在尝试学习如何使用 Web 控制台并在其中进行撬动。我的问题是 binding.pry 不会出现在 destroy 方法中,但会出现。那告诉我它一定是一个坏链接吧?它甚至没有进入销毁方法。谢谢大家的答案。我们确实必须尝试一些事情,不是吗?试

<td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td> 

is not going to work for that one.

不会为那个工作。

Here's what he has

这是他所拥有的

<td><%= link_to 'Destroy', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete %></td>

because you only want to delete the comment here.

因为你只想删除这里的评论。

But combining things so far like the following gives no errors but I still have no binding.pry from the destroy method in the controller.

但是到目前为止,像下面这样组合起来没有错误,但我仍然没有来自控制器中的 destroy 方法的 binding.pry 。

<td><%= link_to 'Destroy', [comment.post, comment], method: :delete, data: { confirm: 'Are you sure?' } %></td>

and I never get the confirmation like you would expect.

我从来没有像你期望的那样得到确认。

So do try to figure out what's going on with the jquery as suspect. I can confirm that was my case but the rest I'll leave for nubes because this will show up in a google search.

所以一定要试着弄清楚 jquery 是怎么回事。我可以确认这是我的情况,但其余的我会留给 nubes,因为这会出现在谷歌搜索中。

回答by Jason Kim

Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600

Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600

This is the problem. The delete link is using GET http verb even though you used method: deletein your link.

这就是问题。即使您method: delete在链接中使用,删除链接也使用 GET http 动词。

Check out the following SO thread

查看以下SO线程

回答by Chitresh goyal

include below line in js

在 js 中包含以下行

//= require jquery_ujs

include gem:

包括宝石:

gem 'jquery-rails'

Destroy Link:

销毁链接:

<%= link_to "Logout", destroy_user_session_path %>

回答by 1jgjgjg

I had the same issue and realized that I had created my rails app with -Jwhich is the same as --skip-javascript. You need JavaScript enabled for this to work; the lack of a popup to confirm the delete is a dead giveaway.

我遇到了同样的问题,并意识到我已经创建-J了与--skip-javascript. 您需要启用 JavaScript 才能工作;缺乏确认删除的弹出窗口是一个死的赠品。

回答by danjonesgh

I had the same problem as Barry. Make sure you're copy/pasting correctly in your /app/views/index.html.erb file and inspect the html that is rendered.

我和巴里有同样的问题。确保您在 /app/views/index.html.erb 文件中正确复制/粘贴并检查呈现的 html。

回答by Barry

I had exactly this problem and it was caused by triple-clicking the Guide's code block and copy/pasting the code straight into my editor (ST2 on OSX).

我正是有这个问题,它是由三次单击指南的代码块并将代码直接复制/粘贴到我的编辑器(OSX 上的 ST2)引起的。

You should check that the generated HTML for the link looks like this:

您应该检查为链接生成的 HTML 是否如下所示:

<a data-confirm="Are you sure?" data-method="delete" href="/posts/3/comments/3" rel="nofollow">Destroy Comment</a>

If it doesn't, but instead looks like the below, then the Javascript won't be applied:

如果不是,而是如下所示,则不会应用 Javascript:

<a href="/posts/3/comments/3" ???????????????data="{:confirm=&gt;&quot;Are you sure?&quot;}" ???????????????method="delete">Destroy Comment</a>

The large gaps between the hrefand the unparsed Ruby are caused by the non-breaking spaces (unicode character \u00a0) used in the Guide being copied into your script.

hrefRuby 和未解析的 Ruby之间的巨大差距是由\u00a0将 Guide 中使用的不间断空格(unicode 字符)复制到您的脚本中造成的。

I'm not sure why this doesn't cause a script parse error.

我不确定为什么这不会导致脚本解析错误。