【问题标题】:Rails delete functionality is not workingRails 删除功能不起作用
【发布时间】:2015-05-08 17:40:02
【问题描述】:

我对 Ruby On Rails 完全陌生,正在通过 this guide 构建一个基本应用程序。

当我尝试实现文档中提到的删除功能时,我看到了显示页面。

我的控制器中有以下方法:

def destroy
  @article = Article.find(params[:id])
  @article.destroy

  redirect_to articles_path
end

在我的页面下面一行:

<td><%= link_to 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>

现在当我点击链接时,我在浏览器中看到以下 URL:

http://localhost:3000/articles/1

现在在这种情况下,我看到的是显示屏幕,而不是收到警报消息,然后从我的页面中删除记录。

我已关注此 SO 帖子 - Rails 4 link_to Destroy not working in Getting Started tutorial

并验证了

//= require jquery
//= require jquery_ujs

元素在我的application.js

请告诉我哪里出错了?

更新:

这是我对rake routes 命令的输出:

E:\Rails\blog\bin>rake routes
(in E:/Rails/blog)
       Prefix Verb   URI Pattern                  Controller#Action
welcome_index GET    /welcome/index(.:format)     welcome#index
     articles GET    /articles(.:format)          articles#index
              POST   /articles(.:format)          articles#create
  new_article GET    /articles/new(.:format)      articles#new
 edit_article GET    /articles/:id/edit(.:format) articles#edit
      article GET    /articles/:id(.:format)      articles#show
              PATCH  /articles/:id(.:format)      articles#update
              PUT    /articles/:id(.:format)      articles#update
              DELETE /articles/:id(.:format)      articles#destroy
         root GET    /                            welcome#index

这是我单击删除链接时从日志文件中得到的信息:

Started GET "/articles/3" for 127.0.0.1 at 2015-05-09 00:56:08 +0530
Processing by ArticlesController#show as HTML
  Parameters: {"id"=>"3"}
  [1m[35mArticle Load (0.0ms)[0m  SELECT  "articles".* FROM "articles"  WHERE "articles"."id" = ? LIMIT 1  [["id", 3]]
  Rendered articles/show.html.erb within layouts/application (0.0ms)
Completed 200 OK in 47ms (Views: 46.9ms | ActiveRecord: 0.0ms)

在我的 application.html.erb 文件中,我已将文本 application 修改为 default 以解决我在这篇文章中报告的问题 - ExecJS::ProgramError in Welcome#index TypeError: Object doesn't support this property or method ,这是我现在问题的原因吗?如果恢复更改,则应用程序完全失败。

【问题讨论】:

  • 查看log/development.log 以查看触发了哪些路由以及生成了哪些错误(如果有)。在开发 Rails 应用程序时,您可能应该一直打开 tail -f log/development.log 或等效项,以便尽早发现此类错误。
  • @tadman 他可能使用rails server 启动了Rails 应用程序。这将启动应用程序并显示开发日志,但是是的......您不应该在日志中看到Started GET "/articles/1" ...,因为您使用的是method: :delete
  • 请向我们展示您在 config/routes.rb 中为这条路线输入的条目
  • 这听起来很像 application.js 文件未包含在布局中。
  • @ilanberci,用路线列表更新了我的问题

标签: ruby-on-rails ruby windows ruby-on-rails-4


【解决方案1】:

您必须使用button_to 而不是link_to 才能使其工作。

参考这篇帖子-Rails 3 - link_to :delete is redirecting to show action with jquery installed,看看Cypher给出的答案。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-07-17
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
相关资源
最近更新 更多