gpt4 book ai didi

ruby-on-rails - 如何在另一个 Controller 范围内制作 Controller 范围?

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:06 26 4
gpt4 key购买 nike

我有一个名为 status 的 Controller ,它有很多方法,例如 firstsecond 等等。

所以,我有一个 routes.rb 文件,如下所示:

scope 'status', controller: :status do
get 'first'
get 'second'
# and so on
end

我也有一个看起来像'/status/states'的路径:

scope :status do
resources :states
end

如何将这些范围合并为一个范围以使用 DRY & clean 代码?

最佳答案

  resource :status, only: [], controller: :status do
resources :states
collection do
get 'first'
get 'second'
get 'third'
end
end

会给你这个:

       status_states GET    /status/states(.:format)          states#index
POST /status/states(.:format) states#create
new_status_state GET /status/states/new(.:format) states#new
edit_status_state GET /status/states/:id/edit(.:format) states#edit
status_state GET /status/states/:id(.:format) states#show
PATCH /status/states/:id(.:format) states#update
PUT /status/states/:id(.:format) states#update
DELETE /status/states/:id(.:format) states#destroy
first_status GET /status/first(.:format) status#first
second_status GET /status/second(.:format) status#second
third_status GET /status/third(.:format) status#third

关于ruby-on-rails - 如何在另一个 Controller 范围内制作 Controller 范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com