Rails自定义路由重定向到显示

时间:2015-08-29 00:47:03

标签: ruby-on-rails

我已创建自定义路由make_winner_pick,但每次单击链接以遵循路径时,控制器默认为show操作。我无法理解我做错了什么,这让我感到疯狂

  

的routes.rb

resources :league_members

get "league_members/make_winner_pick" => "league_members#make_winner_pick", :as => :make_winner_pick
  

调用路径的位置

<%= link_to 'Join League', make_winner_pick_path(league: league.id), method: :get %>
  

控制台

    Started GET "/league_members/make_winner_pick?league=3" for 127.0.0.1 at 2015-08-29 01:33:56 +0100
    Processing by LeagueMembersController#show as HTML
      Parameters: {"league"=>"3", "id"=>"make_winner_pick"}
      User Load (0.9ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" ASC LIMIT 1
      LeagueMember Load (0.5ms)  SELECT  "league_members".* FROM "league_members"  WHERE "league_members"."id" = $1  ORDER BY "league_members"."id" ASC LIMIT 1  [["id", 0]]
    Completed 404 Not Found in 5ms

    ActiveRecord::RecordNotFound (Couldn't find LeagueMember with 'id'=make_winner_pick):
      app/controllers/league_members_controller.rb:68:in `set_league_member'

有谁可以告诉我为什么我的自定义路由没有被触发而Rails默认为#show动作?出于某种原因,它似乎正在寻找ID为league_member

make_winner_pick

感谢您的光临。

2 个答案:

答案 0 :(得分:1)

尝试在资源中嵌套您的路线:

resources :league_members do
  collection do
    get "make_winner_pick" => "league_members#make_winner_pick", :as => :make_winner_pick
  end
end

因为rails将您的路线make_winner_pick识别为id

答案 1 :(得分:0)

它应该是:

<%= link_to 'Join League', league_members_make_winner_pick_path(league: league.id), method: :get %>