0

私は非常に単純なレールアプリを持っています。Card の足場を生成しました。これにより、ID http://app.com/cards/3を使用して特定のルートに移動して記録することができます。

ルートhttp://app.comに移動すると、ランダムな ID を持つランダムなカードを表示するようにルーティングされ、「X」と表示されます ( http://app.com/cards/X )

これは間違っていると確信していますが、Cards Controller に以下を追加しようとしました:

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

誰かが私を正しい方向に導いてくれませんか? 前もって感謝します。

4

3 に答える 3

0

ランダムな方法をこれに変更します。

def random
  @card = Card.find(rand(Card.count)+1)
end

あなたのルートは問題ないようです。

エラーについてTemplate is missingは、おそらく、views/cards/ ディレクトリに random.html.erb または random.html.haml がないためです。

于 2013-04-03T20:47:36.010 に答える