我创建Rails项目与支架 http://localhost:3000/games/如何获得在轨自定义URL 4
当我增加了新的游戏,我得到的网址为 http://localhost:3000/games/first-game
我想改变这 http://localhost:3000/game/first-game 或 http://localhost:3000/first-game
我的路线文件有
resources :games
我创建Rails项目与支架 http://localhost:3000/games/如何获得在轨自定义URL 4
当我增加了新的游戏,我得到的网址为 http://localhost:3000/games/first-game
我想改变这 http://localhost:3000/game/first-game 或 http://localhost:3000/first-game
我的路线文件有
resources :games
如果你想要的资源games
充当根路径,你可以设置它的路径“/”在routes.rb中
resources :games, path: '/'
如果你只希望它为show
动作(如我想你做的),使用方法:
resources :games, path: '/', only: [:show]
resources :games, except: [:show]
(这会给http://localhost:3000/first-game)
或第三这可能是最明智的,以避免冲突的途径,呼吁使用单数“游戏”路径show
行动:
resources :games, path: 'game', only: [:show]
resources :games, except: [:show]
你可以为你的行动像这样创建一个自定义路线:
get '/games/first-game' => 'Games#first_game'
你可以做:
resources :games
member do
get 'first-game', to: 'games#first_game', as: :first_game
end
end
读Naming Routes指南。
嗨mgrim感谢,最后一个选项的作品,但不破坏行动。 – ajin6747
它不?有趣。 (我假设你在两行中都有''':show,:destroy]''')什么是'''$ rake routes'''输出? – mgrim
添加后:销毁它,但编辑停止。 $ rake路由输出: - 前缀动词URI模式控制器#动作 edit_game GET /game/:id/edit(.:format)games#edit GET /game/:id(.:format)games#show DELETE /游戏/:id(。:format)games#destroy 游戏GET /games(.:format)games#index POST /games(.:format)games#create new_game GET /games/new(.:format)games #new PATCH /games/:id(.:format)games#update PUT /games/:id(.:format)games#update – ajin6747