gpt4 book ai didi

ruby-on-rails - Ruby on Rails 中的 Controller 是否有任何部分类型的东西

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:21 25 4
gpt4 key购买 nike

我有一个包含 1000 多行代码的 Controller 。

不对,我正在为此 Controller 进行代码审查。我根据模块安排我的方法。现在我意识到我的 Controller 不容易维护,所以我想要像下面这样的东西

class UsersController < ApplicationController  
#Code to require files here
#before filter code will goes here

#############Here i want to call that partial like things. following is just pseudo #########
history module
account module
calendar module
shipment module
payment module
####################################################################

end #end of class

这对我维护代码有很大帮助,因为当我更改历史模块时,我确信我的帐户模块未更改。我知道 CVS,但我更喜欢每个模块 50 个副本,而不是 users_controller.rb 本身的 200 个副本。

附言:- 我想要肯定的回答。请不要这样回答,你应该为不同的模块使用不同的 Controller .....bla...bla...bla... 因为我不可能这样做。

编辑:- 我的版本是

rails -v
Rails 2.3.4
ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-linux]

最佳答案

这应该适合你:

app/controllers/some_controller.rb

class SomeController < ApplicationController
include MyCustomMethods
end

lib/my_custom_methods.rb

module MyCustomMethods
def custom
render :text => "Rendered from a method included from a module!"
end
end

config/routes.rb

# For rails 3:
match '/cool' => "some#custom"

# For rails 2:
map.cool 'cool', :controller => "some", :action => "custom"

启动您的应用并点击 http://localhost:3000/cool ,您将从模块中获取您的自定义方法。

关于ruby-on-rails - Ruby on Rails 中的 Controller 是否有任何部分类型的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3270756/

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