Ruby on Rails3:如何在路由中包含非标准方法?

时间:2011-01-30 18:22:03

标签: ruby-on-rails ruby-on-rails-3 routing

我的routes.rb:

resources :board
root :to => 'application#index'
match ':controller(/:action(/:id(.:format)))'

在我的BoardController中我有一个名为take_turn

的方法

但是当我点击指向电路板#take_turn的链接时,我收到错误:

Unknown action

The action 'show' could not be found for BoardController

链接如下所示:

http://localhost:3000/board/take_turn?x=0&y=0

3 个答案:

答案 0 :(得分:1)

resources :boards do
  match 'take_turn', :on => :collection
end

默认路径:take_turn_boards_path

答案 1 :(得分:0)

match 'board/take_turn/:x/:y' => "board#take_turn", :as => 'take_turn'

哪个可以take_turn_path

访问

http://guides.rubyonrails.org/routing.html#naming-routes

resources :board之前放置:路由优先级问题

答案 2 :(得分:0)

我假设您的案例中的take_turn是集合上的GET方法。

resources :board do
  collection do
    get :take_turn
  end
  # OR
  get :take_turn, :on => :collection
end

p.s:超出主题..最佳做法是最好关闭通配符路由以防止意外行为:

#match ':controller(/:action(/:id(.:format)))'