2013-04-04 115 views
1

我需要添加到config.rb來解決這個問題。路由錯誤沒有路由匹配{:action =>「reset」,:controller =>「players」}

由於我非常有限的軌道知識,我設法將這種方法放在控制器中。

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 
    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 

回答

1

你可以這樣做:

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

有關路由的更多信息,你可以閱讀文檔here.

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> 
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 

,但我仍然爲什麼你需要,當你點擊 '新遊戲',所有更新假。

玩家 - 它的一個模型。如果您打電話給Player.all - 您將更新所有玩家。

也許你需要事端喜歡game.playes.each(所有玩家對當前的遊戲)

+0

我需要它,因爲我希望球員去剔掉,如果他們是在玩。所以在一場比賽之後,大多數玩家都會玩遊戲。然後,當下一場比賽到來時,它需要重置爲玩=假,以便他們可以說,如果他們正在玩或不玩。 – user1898829 2013-04-04 10:33:39

相關問題