Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails中的控制器有什么不完整的东西吗_Ruby On Rails_Ruby_Partial_Controllers - Fatal编程技术网

Ruby on rails RubyonRails中的控制器有什么不完整的东西吗

Ruby on rails RubyonRails中的控制器有什么不完整的东西吗,ruby-on-rails,ruby,partial,controllers,Ruby On Rails,Ruby,Partial,Controllers,我有一个控制器,有1000多行代码 对不对,我正在为这个控制器做代码检查。 我根据模块安排我的方法。 现在我意识到我的控制器不容易维护,所以我想做如下的事情 class UsersController < ApplicationController #Code to require files here #before filter code will goes here #############Here i want to call that partial li

我有一个控制器,有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

我尝试了以下方法并运行了它,也许它适合您:

app/user_controller.rb

require 'index.rb'
class UsersController < ApplicationController  
  # some other code
end
需要'index.rb'
类UsersController
app/index.rb

class UsersController < ApplicationController

def index
  @users = User.all
end

end
class UsersController

MY ENV:rails 3 beta4这应该适合您:

app/controllers/some\u controller.rb

class SomeController < ApplicationController
  include MyCustomMethods
end
module MyCustomMethods
  def custom
    render :text => "Rendered from a method included from a module!"
  end
end
# For rails 3:
match '/cool' => "some#custom"

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

class SomeController < ApplicationController
  include MyCustomMethods
end
module MyCustomMethods
  def custom
    render :text => "Rendered from a method included from a module!"
  end
end
# For rails 3:
match '/cool' => "some#custom"

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

启动你的应用程序并点击,你会从一个模块中得到你的自定义方法。

假设你的伪代码是指Ruby模块,而不是其他什么,只要把你所有的requires/modules放在一个单独的模块中,并包括它,或者如果你正在重用那些文件,让你的UsersController从基类继承。在第一种情况下,您可以将模块视为混合模块,它的设计正好符合您所需的模块性

module AllMyStuff
  include History
  include Account
  ...
end

class UsersController < ApplicationController
  include AllMyStuff

  def new
  end
  ...
end
模块AllMyStuff
包括历史
包括帐户
...
结束
类UsersController
或者您可以从基本控制器继承,在这种情况下,这可能是一个合理的解决方案

def BaseController < ActionController
  include history
  include account
end

def UsersController < BaseController
  # modules available to this controller by inheritance
  def new
  end
  ...
end
def BaseController
好奇为什么不能更改设计。太多的代码通常是一个很好的迹象,表明您的设计需要工作。@Toby Hede:-如果我为每个模块创建新的控制器,我不想更改每个页面上每个链接的重定向。每个人都应该注意为什么对这个问题给出-1?这很有趣。但是,如果我在lib目录中进行分支,如何包含该模块?例如,如果我有
lib/my\u custom\u methods.rb
,而不是
lib/my\u custom\u methods.rb
,我将如何在控制器中引用后者?您可以在lib文件夹中创建子目录,并将模块存储在其中。规定是,如果您有
lib/custom/methods.rb
,Rails将期望该文件定义模块
custom::methods
。除此之外,模块的创建和引用完全相同。