1

我需要添加什么到 config.rb 来解决这个问题。

由于我对 Rails 的了解非常有限,我设法将此方法放入控制器中。

def  reset
        Player.each do |p|
            p.playing = false
            p.save
        end
   end

并在视图中创建此链接

<p><%= link_to "New Game", {:action => 'reset' }%></p>

我只是不确定在 routes.rb 中放什么来让它工作而不会填满我已经拥有的东西。

这是我的 config.rb

ChooseTeams3::Application.routes.draw do
   resources :players
 root :to => "players#index"
get   "/index" => "players#index"


end

如果我输入 rake routes 我会得到这个

    rake routes
    players GET    /players(.:format)          players#index
            POST   /players(.:format)          players#create
 new_player GET    /players/new(.:format)      players#new
edit_player GET    /players/:id/edit(.:format) players#edit
     player GET    /players/:id(.:format)      players#show
            PUT    /players/:id(.:format)      players#update
            DELETE /players/:id(.:format)      players#destroy
       root        /                           players#index
      index GET    /index(.:format)            players#index
4

3 回答 3

1

你可以做:

ChooseTeams3::Application.routes.draw do
   resources :players do
     get "reset", on: :collection
   end
   root :to => "players#index"
   get   "/index" => "players#index"
end

有关路由的更多信息,您可以在此处阅读文档。

于 2013-04-04T09:14:54.783 回答
0

将您的路线文件更新为..

ChooseTeams3::Application.routes.draw do
   resources :players do
     collection do
       get 'reset'
     end
  end
  root :to => "players#index"
  get   "/index" => "players#index"
end

并像这样使用link_to ..

<p><%= link_to "New Game", reset_players_path%></p>
于 2013-04-04T09:30:00.963 回答
0
ChooseTeams3::Application.routes.draw do
   resources :players do
     get "reset", on: :collection
   end
   root :to => "players#index"
   get   "/index" => "players#index"
end

<p><%= link_to "New Game", reset_players_path %></p>

def  reset
        Player.all.each do |p|
            p.update_attribute(:playing, false)
        end
end

但我仍然为什么需要,当您单击“新游戏”时,将 ALL 更新为 false。

播放器 - 它是一个模型。如果您调用 Player.all - 您将更新所有玩家。

也许你需要类似 game.playes.each 的东西(当前游戏的所有玩家)

于 2013-04-04T09:38:18.773 回答