ruby-on-rails-3 - 当我想要打开或过期的自定义操作时,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"}
在我看来,动作应该是不带参数的
我究竟做错了什么?
解决方案
我想我可能已经回答了我的问题,因为看起来 Rails 每个控制器只能有一个索引,所以我无法定义另一个充当索引的视图。
所以现在我想做的是传递一个参数,这样我就可以应用一个开放的范围或过期的范围
推荐阅读
- c - 在 C 中使用分配右移数组(中止陷阱:6)
- java - 如何在android中将具有多个点的字符串转换为double
- python - 如何使用另一个列表作为键的参考来更改 Python 3.5 字典中键的顺序?
- python - 获取类别中的模型对象
- django - 在 Django 中将日期时间转换为字符串的问题
- django - Django + Heroku:自动迁移需要咨询锁吗?
- node.js - nodejs REPL 输出在同一行
- javascript - 有没有办法为角度表单字段禁用 chrome 自动填充选项
- wordpress - 将 woocommerce 产品排成一行
- powershell - 打印变量到控制台打印一个空值