为什么Rails要为我的#collection#route寻找ID?

时间:2013-02-15 04:18:02

标签: ruby-on-rails routing

我有......

的routes.rb

  resources :standards do
     collection do
        get :none
     end
  end

我通过rake routes获得以下内容:

none_standards GET    /standards/none(.:format)                  standards#none

我的standards_controller.rb中有以下内容:

def none
end

那么为什么我会在/ standards / none中找到“找不到没有ID的标准”错误?

使用better_errors,它说:

(gem) activerecord-3.2.11/lib/active_record/relation/finder_methods.rb
  305 
  306       ids = ids.flatten.compact.uniq
  307 
  308       case ids.size
  309       when 0
  310         raise RecordNotFound, "Couldn't find #{@klass.name} without an ID"
  311       when 1
  312         result = find_one(ids.first)
  313         expects_array ? [ result ] : result
  314       else
  315         find_some(ids)

...

Instance Variables

@table  
#<Arel::Table:0x007fc321207650 @name="standards", @engine=Standard(id: integer, name: string, description: string, created_at: datetime, updated_at: datetime), @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>
@klass  
Standard(id: integer, name: string, description: string, created_at: datetime, updated_at: datetime)

这是一条收集路线,而不是会员路线,所以这看起来很奇怪。

2 个答案:

答案 0 :(得分:1)

问题在于declarative_authorization。我必须在filter_resource_access中将filter_access_to :all(假设常规路线)更改为standards_controller.rb(涵盖所有路线)。

答案 1 :(得分:0)

奇怪!,对我来说,它适用于以下设置(只需检查你的路线说的是什么)

#app/controllers/standards_controller.rb
class StandardsController < ApplicationController
   def none

   end 
end

#config/routes.rb
resources :standards do
  collection  do
    get :none
  end
end

我的路线是

none_standards GET    /standards/none(.:format)     standards#none
     standards GET    /standards(.:format)          standards#index
               POST   /standards(.:format)          standards#create
  new_standard GET    /standards/new(.:format)      standards#new
 edit_standard GET    /standards/:id/edit(.:format) standards#edit
      standard GET    /standards/:id(.:format)      standards#show
               PUT    /standards/:id(.:format)      standards#update
               DELETE /standards/:id(.:format)      standards#destroy
          root        / 
相关问题