首页 > 解决方案 > 当我想要打开或过期的自定义操作时,Rails 会显示操作

问题描述

我基本上是在尝试做一个自定义索引在我的 routes.rb 我有:

resources :tokens do
  resources :visits
  collection do 
    get :open
    get :expired
  end
end

我的路线显示为:

 open_tokens GET    /tokens/open(.:format)                                 tokens#open
 expired_tokens GET    /tokens/expired(.:format)                              tokens#expired

我在令牌控制器中定义了 open 和 expired

 def open

       @q = Token.open_token.includes(:households, :visits).search(params[:q])
    @tokens = @q.result

       respond_to do |format|
       format.html {render template: 'tokens/open'}
       end
  end

   def expired

  @q = Token.expired_token.includes(:households, :visits).search(params[:q])
    @tokens = @q.result

  respond_to do |format|
      format.html {render template: 'tokens/expired'}
   end
  end

但它转到我的显示控制器并出现以下错误:

Couldn't find Token with id=open

它在我的 Show 动作中崩溃:

Better Errors 将请求显示为:

{"action"=>"show", "controller"=>"tokens", "id"=>"open"}

在我看来,动作应该是不带参数的

我究竟做错了什么?

标签: ruby-on-rails-3routes

解决方案


我想我可能已经回答了我的问题,因为看起来 Rails 每个控制器只能有一个索引,所以我无法定义另一个充当索引的视图。

所以现在我想做的是传递一个参数,这样我就可以应用一个开放的范围或过期的范围


推荐阅读