【问题标题】:Routing Error No route matches {:action=>"reset", :controller=>"players"}路由错误没有路由匹配 {:action=>"reset", :controller=>"players"}
【发布时间】:2013-04-04 17:38:18
【问题描述】:

我需要在 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

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    你可以这样做:

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

    有关路由的更多信息,您可以阅读文档here.

    【讨论】:

      【解决方案2】:

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

      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>
      

      【讨论】:

        【解决方案3】:
        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 的东西(当前游戏的所有玩家)

        【讨论】:

        • 我需要它,因为我希望玩家在玩的时候去打勾。所以在一场比赛之后,大多数玩家都会有 play = true。然后当下一场比赛到来时,它需要重置为 play = false 以便他们可以说出他们是否在玩。
        猜你喜欢
        • 2013-03-21
        • 2011-12-25
        • 1970-01-01
        • 1970-01-01
        • 2011-05-06
        • 1970-01-01
        • 2012-07-17
        • 1970-01-01
        • 2011-04-14
        相关资源
        最近更新 更多