ActionController :: RoutingError没有路由匹配[DELETE]

时间:2016-04-30 23:35:18

标签: ruby-on-rails actioncontroller

我正在撰写一个显示新闻Feed但无法删除帖子的页面。当我试图删除某个帖子时,它会给我一个错误:

ActionController :: RoutingError(没有路由匹配[DELETE]“/news.305”):

例如

(305是id)

我做错了什么?

这是我的news_controller

class NewsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :admin_user, only: [:create, :destroy]

def news_page
  @news = News.paginate(page: params[:page], per_page: 10)
  if logged_in?
    @news_post = current_user.news.build if current_user.admin?
    @news_feed = News.paginate(page: params[:page], per_page: 8)
  end
end

def create
  @news_post = current_user.news.build(news_params)
  if @news_post.save
    flash[:success] = "News updated!"
    redirect_to news_url
  else
    @news_feed = []
    render "news/news_page"
  end
end

def destroy
  News.find(params[:id]).destroy
  flash[:success] = "News deleted"
  redirect_to news_url
end

private

  def news_params
    params.require(:news).permit(:content, :news_title)
  end

以下是路线:

           Prefix Verb   URI Pattern            Controller#Action
     password_resets_new GET    /password_resets/new(.:format)      password_resets#new
    password_resets_edit GET    /password_resets/edit(.:format)         password_resets#edit
                    root GET    /                                       static_pages#home
                 history GET    /history(.:format)                      static_pages#history
                    help GET    /help(.:format)                         static_pages#help
                 contact GET    /contact(.:format)                      static_pages#contact
                   about GET    /about(.:format)                        static_pages#about
               downloads GET    /downloads(.:format)                    static_pages#downloads
                    news GET    /news(.:format)                         news#news_page
                  signup GET    /signup(.:format)                       users#new
                testpage GET    /testpage(.:format)                     static_pages#testpage
   delete_inactive_users GET    /delete_inactive_users(.:format)        users#delete_inactive_users
                   login GET    /login(.:format)                        sessions#new
                         POST   /login(.:format)                        sessions#create
                  logout DELETE /logout(.:format)                       sessions#destroy
                   users GET    /users(.:format)                        users#index
                         POST   /users(.:format)                        users#create
                new_user GET    /users/new(.:format)                    users#new
               edit_user GET    /users/:id/edit(.:format)               users#edit
                    user GET    /users/:id(.:format)                    users#show
                         PATCH  /users/:id(.:format)                    users#update
                         PUT    /users/:id(.:format)                    users#update
                         DELETE /users/:id(.:format)                    users#destroy
 edit_account_activation GET    /account_activations/:id/edit(.:format) account_activations#edit
         password_resets POST   /password_resets(.:format)              password_resets#create
      new_password_reset GET    /password_resets/new(.:format)          password_resets#new
     edit_password_reset GET    /password_resets/:id/edit(.:format)     password_resets#edit
          password_reset PATCH  /password_resets/:id(.:format)          password_resets#update
                         PUT    /password_resets/:id(.:format)          password_resets#update
              news_index POST   /news(.:format)                         news#create
                         DELETE /news/:id(.:format)                     news#destroy

以下是完整的错误代码:

Started DELETE "/news.305" for 209.150.49.143 at 2016-04-30 23:07:52 +0000

ActionController::RoutingError (No route matches [DELETE] "/news.305"):
  web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
  web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.2) lib/rails/engine.rb:518:in `call'
  railties (4.2.2) lib/rails/application.rb:164:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (2.6ms)
  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/routing_error.html.erb within rescues/layout (127.3ms)

这是route.rb

Rails.application.routes.draw do

  get 'password_resets/new'
  get 'password_resets/edit'

  root 'static_pages#home'
  get 'history' => 'static_pages#history'
  get 'help' => 'static_pages#help'
  get 'contact' => 'static_pages#contact'
  get 'about' => 'static_pages#about'
  get 'downloads' => 'static_pages#downloads'
  get "news" => 'news#news_page'
  get 'signup' => 'users#new'
  get 'testpage' => 'static_pages#testpage'
  get 'delete_inactive_users' => 'users#delete_inactive_users'

  get 'login' => 'sessions#new'
  post 'login' => 'sessions#create'
  delete 'logout' => 'sessions#destroy'

  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets, only: [:new, :create, :edit, :update]
  resources :news, only: [:create, :destroy]
end

以下是包含删除的部分:

<% if @news_feed.any? %>
  <ol class="news news_feed">
  <% @news_feed.each do |news| %>
      <li id="news-<%= news.id %>">
        <aside class="col-md-2">
          <span class="news_title"><%= news.news_title %></span>  
        </aside>
        <span class="news_content"><%= news.content %></span>
        <span class="user"><%= link_to news.user.name, news.user %></span>
        <span class="timestamp">
          Posted on <%= news.created_at.to_time.strftime('%m/%d/%Y') %>
          <%= link_to "delete", news, method: :delete,
                            data: { confirm: "Delete this news item?" } %>
        </span>
      </li>
  <% end %>
  </ol>
  <%= will_paginate %>
<% end %>

2 个答案:

答案 0 :(得分:2)

尝试传入实际的新闻ID:

<%= link_to 'Destroy',  { action: :destroy, id: news.id }, 
       method: :delete,
       data: { confirm: "Delete this news item?" } 
%>

答案 1 :(得分:1)

确定!我发现了这个问题。显然,我不应该将我的模型命名为“新闻”。因为它链接到用户模型,用户可以has_many新闻。当我执行user.news之类的操作时,它会产生名称冲突。我重新创建了一个名为“Newspost”的模型,所以现在user.newsposts不会混淆rails。现在我可以毫无问题地删除。