Rails :: method => :删除寻找'show'动作

时间:2012-02-14 07:04:51

标签: ruby-on-rails

我已将Enki(博客)gem https://github.com/xaviershay/enki与使用Devise进行注册的Forem(forums)gem https://github.com/radar/forem一起安装。除非我点击链接

,否则一切似乎都有效
<li><%= link_to t('delete', :scope => 'forem.topic'), topic_post_path(@topic, post), :method => :delete, :confirm => t("are_you_sure") %></li>

删除论坛内的帖子(或其他任何内容),我收到此错误消息

Unknown action

The action 'show' could not be found for Forem::PostsController

我注意到Forem链接有:method => :delete所以这不应该发生。此外,当我使用没有Enki的Forem时,我没有遇到这个问题。我认为由于某种原因,rails应用程序没有看到:method =&gt; :删除所以该方法被注册为“show”而不是destroy动作,但我不知道为什么。在日志文件中它说

* REQUEST_METHOD : GET –

我猜测路由中存在某种冲突,导致禁用:method =&gt; :删除,但我无法弄明白。

配置/ routes.rb中

Enki::Application.routes.draw do
  mount Forem::Engine, :at => "/forums"
  devise_for :users

  namespace 'admin' do
    resource :session

    resources :posts, :pages do
      post 'preview', :on => :collection
    end
    resources :comments
    resources :undo_items do
      post 'undo', :on => :member
    end

    match 'health(/:action)' => 'health', :action => 'index', :as => :health

    root :to => 'dashboard#show'
  end

  resources :archives, :only => [:index]
  resources :pages, :only => [:show]

  constraints :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ do
    get ':year/:month/:day/:slug/comments'  => 'comments#index'
    post ':year/:month/:day/:slug/comments' => 'comments#create'
    get ':year/:month/:day/:slug/comments/new' => 'comments#new'
    get ':year/:month/:day/:slug' => 'posts#show'
  end

  scope :to => 'posts#index' do
    get 'posts.:format', :as => :formatted_posts
    get '(:tag)', :as => :posts
  end

  root :to => 'posts#index'
end

佣金路线

forem        /forums                                         {:to=>Forem::Engine}
        new_user_session GET    /users/sign_in(.:format)                        {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)                        {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)                       {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)                       {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)                   {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format)                  {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)                       {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)                         {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)                                {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)                        {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)                           {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)                                {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)                                {:action=>"destroy", :controller=>"devise/registrations"}
           admin_session POST   /admin/session(.:format)                        {:action=>"create", :controller=>"admin/sessions"}
       new_admin_session GET    /admin/session/new(.:format)                    {:action=>"new", :controller=>"admin/sessions"}
      edit_admin_session GET    /admin/session/edit(.:format)                   {:action=>"edit", :controller=>"admin/sessions"}
                         GET    /admin/session(.:format)                        {:action=>"show", :controller=>"admin/sessions"}
                         PUT    /admin/session(.:format)                        {:action=>"update", :controller=>"admin/sessions"}
                         DELETE /admin/session(.:format)                        {:action=>"destroy", :controller=>"admin/sessions"}
     preview_admin_posts POST   /admin/posts/preview(.:format)                  {:action=>"preview", :controller=>"admin/posts"}
             admin_posts GET    /admin/posts(.:format)                          {:action=>"index", :controller=>"admin/posts"}
                         POST   /admin/posts(.:format)                          {:action=>"create", :controller=>"admin/posts"}
          new_admin_post GET    /admin/posts/new(.:format)                      {:action=>"new", :controller=>"admin/posts"}
         edit_admin_post GET    /admin/posts/:id/edit(.:format)                 {:action=>"edit", :controller=>"admin/posts"}
              admin_post GET    /admin/posts/:id(.:format)                      {:action=>"show", :controller=>"admin/posts"}
                         PUT    /admin/posts/:id(.:format)                      {:action=>"update", :controller=>"admin/posts"}
                         DELETE /admin/posts/:id(.:format)                      {:action=>"destroy", :controller=>"admin/posts"}
     preview_admin_pages POST   /admin/pages/preview(.:format)                  {:action=>"preview", :controller=>"admin/pages"}
             admin_pages GET    /admin/pages(.:format)                          {:action=>"index", :controller=>"admin/pages"}
                         POST   /admin/pages(.:format)                          {:action=>"create", :controller=>"admin/pages"}
          new_admin_page GET    /admin/pages/new(.:format)                      {:action=>"new", :controller=>"admin/pages"}
         edit_admin_page GET    /admin/pages/:id/edit(.:format)                 {:action=>"edit", :controller=>"admin/pages"}
              admin_page GET    /admin/pages/:id(.:format)                      {:action=>"show", :controller=>"admin/pages"}
                         PUT    /admin/pages/:id(.:format)                      {:action=>"update", :controller=>"admin/pages"}
                         DELETE /admin/pages/:id(.:format)                      {:action=>"destroy", :controller=>"admin/pages"}
          admin_comments GET    /admin/comments(.:format)                       {:action=>"index", :controller=>"admin/comments"}
                         POST   /admin/comments(.:format)                       {:action=>"create", :controller=>"admin/comments"}
       new_admin_comment GET    /admin/comments/new(.:format)                   {:action=>"new", :controller=>"admin/comments"}
      edit_admin_comment GET    /admin/comments/:id/edit(.:format)              {:action=>"edit", :controller=>"admin/comments"}
           admin_comment GET    /admin/comments/:id(.:format)                   {:action=>"show", :controller=>"admin/comments"}
                         PUT    /admin/comments/:id(.:format)                   {:action=>"update", :controller=>"admin/comments"}
                         DELETE /admin/comments/:id(.:format)                   {:action=>"destroy", :controller=>"admin/comments"}
    undo_admin_undo_item POST   /admin/undo_items/:id/undo(.:format)            {:action=>"undo", :controller=>"admin/undo_items"}
        admin_undo_items GET    /admin/undo_items(.:format)                     {:action=>"index", :controller=>"admin/undo_items"}
                         POST   /admin/undo_items(.:format)                     {:action=>"create", :controller=>"admin/undo_items"}
     new_admin_undo_item GET    /admin/undo_items/new(.:format)                 {:action=>"new", :controller=>"admin/undo_items"}
    edit_admin_undo_item GET    /admin/undo_items/:id/edit(.:format)            {:action=>"edit", :controller=>"admin/undo_items"}
         admin_undo_item GET    /admin/undo_items/:id(.:format)                 {:action=>"show", :controller=>"admin/undo_items"}
                         PUT    /admin/undo_items/:id(.:format)                 {:action=>"update", :controller=>"admin/undo_items"}
                         DELETE /admin/undo_items/:id(.:format)                 {:action=>"destroy", :controller=>"admin/undo_items"}
            admin_health        /admin/health(/:action)(.:format)               {:action=>"index", :controller=>"admin/health"}
              admin_root        /admin(.:format)                                {:controller=>"admin/dashboard", :action=>"show"}
                archives GET    /archives(.:format)                             {:action=>"index", :controller=>"archives"}
                    page GET    /pages/:id(.:format)                            {:action=>"show", :controller=>"pages"}
                         GET    /:year/:month/:day/:slug/comments(.:format)     {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"comments", :action=>"index"}
                         POST   /:year/:month/:day/:slug/comments(.:format)     {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"comments", :action=>"create"}
                         GET    /:year/:month/:day/:slug/comments/new(.:format) {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"comments", :action=>"new"}
                         GET    /:year/:month/:day/:slug(.:format)              {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"posts", :action=>"show"}
         formatted_posts GET    /posts.:format                                  {:controller=>"posts", :action=>"index"}
                   posts GET    /(:tag)(.:format)                               {:controller=>"posts", :action=>"index"}
                    root        /                                               {:controller=>"posts", :action=>"index"}

4 个答案:

答案 0 :(得分:5)

确保您的JavaScript包含在布局文件中:

<%= javascript_include_tag :defaults %> #Rails 3.0.x
<%= javascript_include_tag :application %> #Rails 3.1+
<%= csrf_meta_tag %>

对于删除,您应该使用button_to代替。 Link_to不应用于HTTP POST请求。

答案 1 :(得分:0)

您需要在Gemfile中包含jQuery for Rails gem

gem 'jquery-rails'

然后你必须运行bundle install并重启服务器。这是因为即使你已经在layout.html.erb中包含了jQuery框架,你需要jQuery的适配器来使用不引人注目的脚本。

答案 2 :(得分:0)

这是因为你的javascript中没有加载rails jquery_ujs文件。

在布局文件中执行此操作,通常命名为 application.html.erb

<%= javascript_include_tag "application" %>

这将包括application.js文件,还包括你的jquery_ujs文件

您应该检查app / assets / application.js文件,该行存在

//= require jquery_ujs

如果不自行添加。

它会治愈:)。

<强>提示:

其中一个可能是问题的原因:

1-当您不使用application.js时可能会导致

2-您正在使用不同的文件来包含库而忘记了 jquery_ujs

答案 3 :(得分:-2)

看起来你在该控制器中没有方法show。只需定义它。