【问题标题】:Ruby On Rails custom route always redirects to controller's show actionRuby On Rails 自定义路由总是重定向到控制器的显示动作
【发布时间】:2013-07-26 00:49:33
【问题描述】:

我正在尝试创建一条新路线,以便我可以利用 RoR 的路径变量功能,即 new_game_path。就我而言,我想使用 load_game_path

我已经为合适的控制器创建了一个动作,目前路由如下:

resources :games do
    get 'load', on: :collection
end

每次我使用 load_games_path 时,它都会使用正确的 URI,但似乎会重定向到 GamesController 的显示操作并显示游戏的继承显示视图。

我检查了 rake 路线,我看到我新创建的路线似乎是所需的路径 /games/load(文件路径:/views/games/load.html.erb)

load_games GET    /games/load(.:format)       games#load/

耙子路线:

    welcome_index GET    /welcome/index(.:format)    welcome#index
      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
        games GET    /games(.:format)            games#index
              POST   /games(.:format)            games#create
     new_game GET    /games/new(.:format)        games#new
    edit_game GET    /games/:id/edit(.:format)   games#edit
         game GET    /games/:id(.:format)        games#show
              PUT    /games/:id(.:format)        games#update
              DELETE /games/:id(.:format)        games#destroy
        users GET    /users(.:format)            users#index
              POST   /users(.:format)            users#create
     new_user GET    /users/new(.:format)        users#new
    edit_user GET    /users/:id/edit(.:format)   users#edit
         user GET    /users/:id(.:format)        users#show
              PUT    /users/:id(.:format)        users#update
              DELETE /users/:id(.:format)        users#destroy
                     /players/:name(.:format)    players#index
   load_games GET    /games/load(.:format)       games#load
              GET    /games(.:format)            games#index
              POST   /games(.:format)            games#create
              GET    /games/new(.:format)        games#new
              GET    /games/:id/edit(.:format)   games#edit
              GET    /games/:id(.:format)        games#show
              PUT    /games/:id(.:format)        games#update
              DELETE /games/:id(.:format)        games#destroy
         root        /                           welcome#index

routes.rb:

     get "welcome/index"

     resources :players, :games, :users

     match '/players/:name'  => 'players#index'

     # match 'games/load(.:format)', :controller => 'games', :action => 'load'

      resources :games do
        collection do
          get 'load'
        end
      end

root :to => 'welcome#index'

我知道加载是控制器的预定义操作。为了确保这不是问题,我尝试了一个任意名称的操作 - 产生相同的结果。

我也试过了,但没有成功:

match 'games/load(.:format)', :controller => 'games', :action => 'load'

【问题讨论】:

  • 日志说什么?它会告诉你正在调用哪个动作。
  • 这里是日志的sn-p。您可以看到它正在尝试调用 show 操作。不确定 HTML id => 'load' 参数的含义。 ' 在 2013 年 7 月 25 日 21:07:36 -0400 开始 GET "/games/load" for 127.0.0.1 由 GamesController#show 作为 HTML 参数处理:{"id"=>"load"} 完成 500 内部服务器错误在 4ms ActionView::MissingTemplate (缺少模板游戏/节目,应用程序/节目与 {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee] }. 搜索:* "W:/Rails/testApp/blog/app/views" ): actionpack '
  • 你能把rake routes的结果粘贴到这里吗?只有那些与游戏相关且具有持久顺序的。因为我在想你是否有可能有另一条路径“无意”抓住了这条路线并传递给展示行动
  • 当然。我现在在原始帖子中包含了 rake 路线

标签: ruby-on-rails ruby routing


【解决方案1】:

试试

resources :games do
  collection do
    get 'load'
  end
end

现在它将 'games/load' 解释为 'games/:id' 并将 :id 参数设置为 'load',并将 'games/:id' 路由到 GamesController#show。

编辑:并确保在路由文件的前面没有再次调用 resources :games,即使 :games 只是像 resources :players, :games 这样的几个参数之一,因为稍后您将无法插入收集方法如果有的话。

【讨论】:

  • 同样的结果。我也尝试过 member do 并返回以下错误:No route matches {:action=>"load", :controller=>"games"} 我明白你在说什么,这一定是它调用 show 动作的原因?因为它认为我正在尝试显示特定的“游戏”?当我真的只想显示游戏的特定子页面时。
  • 通过收集,您是否得到相同的结果,包括日志中的相同消息(由 GamesController#show 处理)?
  • collection 确实产生了相同的结果:Missing template games/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "W:/Rails/testApp/blog/app/views" 日志显示 GamesController#show 处理
  • 就像你说的,我认为系统认为我正在尝试通过游戏,即 games/1/show.html.erb。实际上,我只想显示 /games/ (../games/load.html.erb) 的子页面。没想到这么难,一定是我做错了。
  • 我打赌你的问题是resources :players, :games, :users - :games 在该列表中,稍后当你尝试添加收集方法时它会忽略重新定义。将顶行更改为resources :players, :users,您就很成功了。
猜你喜欢
  • 2012-09-05
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多