我是Rails的新手。这里试图替换URL中的单词。我的routes.rb
是:
resources :senders, only: [:index, :update]
get '/senders/:id', constraints: { :id => /[0-9]+/ }, to: 'senders#show'
put '/senders/:id', constraints: { :id => /[0-9]+/ }, to: 'senders#update'
get '/senders/:id/check_status/:ip_index', constraints: { :id => /[0-9]+/, :ip_index => /[0-3]/ }, to: 'senders#check_status'
它在网址链接中显示为
localhost:3000/senders
但我希望它显示为
localhost:3000/receivers
我需要对显示接收器/ 173进行哪些更改?
答案 0 :(得分:2)
在不更改控制器中的任何内容的情况下,您可以执行以下操作:
resources :receivers, only: [:index, :update], controller: 'senders'
get '/receivers/:id', constraints: { :id => /[0-9]+/ }, to: 'senders#show'
put '/receivers/:id', constraints: { :id => /[0-9]+/ }, to: 'senders#update'
get '/receivers/:id/check_status/:ip_index', constraints: { :id => /[0-9]+/, :ip_index => /[0-3]/ }, to: 'senders#check_status'
但您还应更改控制器名称并执行以下操作:
首先更改您的控制器名称
将app/controllers/senders_controller.rb
重命名为app/controllers/receivers_controller.rb
并更改控制器中的第一行:
SendersController < ApplicationController
至ReceiversController < ApplicationController
然后最后,更改您的config/routes.rb
,如下所示:
resources :receivers, only: [:index, :update]
get '/receivers/:id', constraints: { :id => /[0-9]+/ }, to: 'receivers#show'
put '/receivers/:id', constraints: { :id => /[0-9]+/ }, to: 'receivers#update'
get '/receivers/:id/check_status/:ip_index', constraints: { :id => /[0-9]+/, :ip_index => /[0-3]/ }, to: 'receivers#check_status'
您可以从documentation了解有关Rails路由的更多信息。