2

1000 行を超えるコードを持つコントローラーがあります。

このコントローラーのコードレビューを行っているわけではありません。モジュールに従ってメソッドを配置します。今、コントローラーの保守が容易ではないことに気付いたので、次のようなことをしたいと思います

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 は知っていますが、users_controller.rb 自体の 200 コピーではなく、各モジュールの 50 コピーを好みます。

PS :- 肯定的な回答をお願いします。別のモジュールには別のコントローラーを使用する必要があります.....bla...bla...bla...のように答えないでください。 .

編集: - 私のバージョンは

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

3 に答える 3

5

これはあなたのために働くはずです:

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を押すと、モジュールからカスタムメソッドが含まれます。

于 2010-07-17T10:04:07.080 に答える
1

疑似コードから、Ruby モジュールを参照していて、他のものではないと仮定すると、すべての require/modules を別のモジュールに入れ、それを含めるか、それらのファイルを再利用している場合は、UsersController を基本クラスから継承させます。最初のケースでは、モジュールをミックスインと考えることができ、まさに必要なモジュール性のために設計されています。

module AllMyStuff
  include History
  include Account
  ...
end

class UsersController < ApplicationController
  include AllMyStuff

  def new
  end
  ...
end

または、ベースコントローラーから継承できます。この場合、おそらく合理的な解決策です。

def BaseController < ActionController
  include history
  include account
end

def UsersController < BaseController
  # modules available to this controller by inheritance
  def new
  end
  ...
end
于 2010-07-17T10:25:15.250 に答える
0

私は次の方法を試しましたが、実行しています。おそらくあなたに適しています:

app/user_controller.rb

require 'index.rb'
class UsersController < ApplicationController  
  # some other code
end

app/index.rb

class UsersController < ApplicationController

def index
  @users = User.all
end

end

私の環境: レール 3 ベータ 4

于 2010-07-17T08:37:41.287 に答える