开发者

Ruby on Rails3: how do I include non-standard methods in the routes?

My rout开发者_如何学Ces.rb:

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

in my BoardController I have a method called take_turn

but when I click on a link that points to board#take_turn, I get the error:

Unknown action

The action 'show' could not be found for BoardController

The link looks like this:

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


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

Path by default: take_turn_boards_path


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

Which would be accessible as take_turn_path

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

put if before resources :board: question of route priority


I assume that take_turn in your case is a GET method on a collection.

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

p.s: out of the subject.. the best practice says that it is better to switch off wild-card routes to prevent unexpected behavior:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜