【问题标题】:Rails The action 'show' could not be found for CommentsController errorRails 找不到 CommentsController 错误的操作“显示”
【发布时间】:2012-05-12 13:14:46
【问题描述】:

我正在尝试按照 rails 入门指南 http://guides.rubyonrails.org/getting_started.html 为我在 rails 中开发的一个小型网站创建一个博客

我已经阅读了许多关于 SO 的类似问题,但我仍然无法弄清楚为什么我在尝试按照指南中所做的那样销毁评论时不断收到错误 The action 'show' could not be found for CommentsController error

这是我的 html.erb 代码中的链接:

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

我的模板标题有这些行

<%= csrf_meta_tag %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", "rails", "superfish", "jquery.quicksand", 'slides.min.jquery' %>

我的 gemfile 包含这个:

gem 'jquery-rails'

我还注意到,当我加载带有销毁评论链接的页面时,firebug 控制台中出现此错误:

"NetworkError: 404 Not Found - http://localhost:3000/assets/rails.js"

对于解决此问题的任何帮助表示赞赏。让我知道您是否想要发布其他任何内容。

编辑: 清单文件。

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

Routes.rb

Allthingswebdesign::Application.routes.draw 做

get "about/index"
get "home/index"
get "portfolio/index"
get "contact/index"
post "contact/create"
get "contact/new"
get "posts/index"
get "posts/new"

资源:帖子做 资源:cmets 结束

root :to => 'home#index' 结束

【问题讨论】:

  • 在添加 gem 之后安装了 run bundle 吗?
  • 是的,我在添加 gem 后运行了 bundle install。
  • 如果没有 rails.js 文件,链接将不会被转换为使用 delete 方法,并且确实会路由显示。只是想确认加载此资产是问题
  • 如果您使用资产管道,那么您应该使用清单来定义所有 javascript 包含标签。
  • 您能否提供有关 rails.js 文件以及资产管道和清单的更多详细信息?我是新来的铁路。 jquery-rails gem 不是包含 rails.js 文件吗?

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

您是否有理由摆脱教程中包含的 javascript? (不是一个尖刻的问题——只是好奇)。尝试将此行添加到app/views/layouts/application.html.erb.

&lt;%= javascript_include_tag "application" %&gt;

您遇到的问题真的很像伪造发送 DELETE 请求的 javascript 函数丢失/故障。

【讨论】:

  • 原来如此。我添加了这个,效果很好。我猜我在将模板放入 Rails 代码时不小心删除了它。
【解决方案2】:

在你的路线中试试这个

resources :posts do
  resources :comments, :only => [:create, :destroy]
end

【讨论】:

  • 现在我在单击显示No route matches [GET] "/posts/2/comments/1" 的删除链接时收到错误消息。我需要在我的路线文件中添加一些东西吗?
  • 好吧,由于某种原因,您的评论删除链接正在发送获取请求而不是删除。我不知道为什么,但我认为如果你在你的路由中添加了一些东西,比如match 'posts/:post_id/comments/:id' =&gt; 'comments#destroy',这将强制链接使用你的 cmets_controller.rb 的 destroy 方法。抱歉,这更像是一种解决方法,而不是“正确的方法”,但是您发布的代码中的所有内容对我来说都很好。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多