路由错误没有路由匹配{:action =>“reset”,:controller =>“players”}

时间:2013-04-04 09:12:06

标签: ruby-on-rails routes

我需要添加什么来配置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路线,我会得到这个

    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

3 个答案:

答案 0 :(得分:1)

你可以这样做:

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

有关路由的详细信息,请参阅文档here.

答案 1 :(得分: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>

答案 2 :(得分: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(当前游戏的所有玩家)一样的东西