Correct order of resources in Rails routes.rb

2019-06-14 11:17发布

问题:

I keep getting strange errors because of the way my routes.rb file is organized. The latest one is that some function cannot find action "show" in model Relations controller (the action is obviously there). I guess this is because I am adding some custom actions via collection and something about the order in which the routes are declared is messed up.. Can somebody please have a look at this and say what is wrong?

YApp::Application.routes.draw do

  require 'resque/server'

  match 'login' => 'user_sessions#new', :as => :login
  match 'logout' => 'user_sessions#destroy', :as => :logout
  match '/get_idx',  :to => 'nodes#get_idx'


  resource :relations do
    collection do
      post 'this_relation'
      post "iframize"
    end
  end


  resource :words do
  get 'page/:page', :action => :index, :on => :collection
    collection do
      get 'front'
      get 'index'
    end
  end

    resource :recommendations do
      collection do
        get 'find_votes'
      end
    end


  get "connotation/create"

  get "connotation/edit"

  get "connotation/update"

  root :to => "words#front", :as => :homepage

  resources :users, :user_sessions, :relations,  :evaluation, :phrases, :metawords, :nodes, :recommendations, :words


  mount Resque::Server.new, :at => "/resque"
  match 'about' => 'words#index' , :as => :about
  match 'contact' => 'keywords#index' , :as => :contact


end

回答1:

You might have an issue with resource :relations. Rule of thumb is: if you use the plural resources, then the name of the resource must also be plural (i.e. :relations), if you use resource, in singular, than you should use singular for the resource name too (i.e. :relation).

Other possible problems: your indentation is off. Maybe it's just a copy-paste issue, but check it nonetheless, because you might have some unexpected nesting going on.

Also inspect rake routes CONTROLLER=relations. Compare that to the log of the failed request and see if every parameter matches up.