我有一个非常简单的 Rails 应用程序。我为 Card 生成了脚手架,除其他外,我可以使用 ID http://app.com/cards/3转到特定路线进行记录。
当我去根http://app.com时,我想被路由以显示带有随机 id 的随机卡,说“X”(http://app.com/cards/X)
我很确定这是错误的,但我尝试在卡片控制器中添加以下内容:
def random
@card = Card.order("RANDOM()").first
end
然后添加 routes.rb:
root :to => 'cards#random'
当我尝试去路由时,我在浏览器中遇到的错误:
Template is missing
Missing template cards/random, application/random with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/patricia/Documents/Repos/coastguard-quiz-rails/app/views"
最后,当我运行“rake routes”时,我得到:
cards GET /cards(.:format) cards#index
POST /cards(.:format) cards#create
new_card GET /cards/new(.:format) cards#new
edit_card GET /cards/:id/edit(.:format) cards#edit
card GET /cards/:id(.:format) cards#show
PUT /cards/:id(.:format) cards#update
DELETE /cards/:id(.:format) cards#destroy
root / cards#random
有人可以指导我正确的方向吗?谢谢你的建议。