【问题标题】:ruby on rails 4.2.6 "delete action" not workingruby on rails 4.2.6“删除操作”不起作用
【发布时间】:2016-06-20 06:47:34
【问题描述】:

我正在开发 ruby​​ on rails 4.2.6,这是我的 show.html 文件

<h1><%= @post.title %></h1>
<p><%= @post.body %></p>
<p><%= @post.created_at %></p>
<%= link_to "Edit Post",edit_post_path %> | <%=link_to 'Delete Post',@post,method: :delete %>

这是我在帖子控制器中的代码

def destroy
 @post = Post.find(params[:id])
 @post.destroy
 redirect to post_path,:notice =>"your post has been deleted"
 end
end

日期没有被删除,它正在显示,命令提示符里面的错误是

action controller::routing error( no routing matches      [get]"/javascripts/default.js)

请帮助!

【问题讨论】:

  • 您使用的是Windows系统吗?
  • 是的,我使用的是 Windows 7。
  • 摆脱你的 Ruby on Rails 开发的 windows 盒子,尝试虚拟盒子或任何在线平台,如 c9.io - 仅供参考:问题是 DELETE 请求不是在 windows 中发出的,而是 @ 987654326@ 请求发送到服务器,它混淆了要执行的操作。而且,问题又出现了,因为您已将 application.html.erb 文件更改为加载 default.js 而不是 application.js
  • 能否提供routes.rb
  • Rails.application.routes.draw 获取 'categories/index' 获取 'categories/edit' 获取 'categories/new' 获取 'categories/show' 获取 'home/index' 资源 :posts 资源:类别结束

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


【解决方案1】:

destroy方法应该是:

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  redirect_to posts_path, :notice => "your post has been deleted"
end

【讨论】:

    【解决方案2】:

    1)。该错误并不意味着“删除操作”不起作用,而是说在您的 html/erb 中的某处,您想要加载“/javascripts/default.js”,但您的config/routes.rb 中没有此 URL 的定义

    2)。您提供的destroy 控制器代码有一个多余的end,应该是这样的:

    def destroy
      @post = Post.find(params[:id])
      @post.destroy
      redirect to post_path, notice: "your post has been deleted"
    end
    

    3)。我认为您的服务器应该为该语法错误记录一些异常,每当出现问题时,日志文件应该是您检查的前几件事。

    【讨论】:

    • 嗯,我认为你的第一点看起来很相关,你能解释一下如何去做吗?我是新来的 。 !!
    • 您能否将服务器日志文件粘贴到您的问题中?或者请求从未发送到服务器?
    • 开始 GET "/javascripts/default.js" for ::1 at 2016-06-20 13:52:54 +0530 ActionController::RoutingError (没有路由匹配 [GET] "/javascripts/ default.js"):
    • 那么问题是您没有正确加载 JavaScript 文件,并且该点击根本不会触发对后端的请求。
    • 那么我应该怎么做呢?我已经更改了我的 application.html.erb 我已将“应用程序”更改为“默认”!
    【解决方案3】:

    在你的控制器中

    redirect to post_path
    

    你重定向到显示页面是错误的,你需要在索引页面上重定向。

    redirect to posts_path
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2014-02-08
      • 1970-01-01
      • 2015-10-16
      • 2016-01-19
      相关资源
      最近更新 更多