Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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_Ruby On Rails 3_Module_Implementation - Fatal编程技术网

Ruby on rails 关于在RubyonRails中使用模块的建议

Ruby on rails 关于在RubyonRails中使用模块的建议,ruby-on-rails,ruby,ruby-on-rails-3,module,implementation,Ruby On Rails,Ruby,Ruby On Rails 3,Module,Implementation,我正在使用RubyonRails3,我知道在哪种情况下使用模块是好的 我有一个控制器,其中包括许多我以这种方式使用的私有方法: class UsersController < ApplicationController def update params[:option1] = get_user_option1 params[:option2] = get_user_option2 if params[:option2] params[:sav

我正在使用RubyonRails3,我知道在哪种情况下使用模块是好的

我有一个控制器,其中包括许多我以这种方式使用的私有方法:

class UsersController < ApplicationController

  def update
    params[:option1] = get_user_option1
    params[:option2] = get_user_option2


    if params[:option2]
      params[:saving_success] = update_user
    end

    ...

    if params[:saving_success] 
      flash[:notice] = another_method_1
    else
      flash[:error] = another_method_2
    end
  end


    private

      def update_user
        if params[:option1] == something
          @user.save
        end
      end

      def another_method_1
        params[...] = ...
      ...
  end
class UsersController
正如您所看到的,在私有方法中,我有类似ActiveRecords和params的方法。我知道在模块中不能直接使用那些ActiveRecordsparams方法,但可以将它们作为参数传递,如本例所示:

# In the controller file
class UsersController < ApplicationController
  include Users

  def update
    params[:option] = "true"
    @users = Users.find(1)
    Users::Validations.name (@user, params[:option])
    ...
  end
end

# In the module file
module Users
  module Validations
    def Validations.name(user, param)
      user == "Test_name" if param
      # Normally the following is not possible:
      # @user == "Test_name" if params[:option]
    end
  end
end
控制器文件中的
#
类UsersController
那么,你对我的情况有什么建议?使用单独的模块好吗?


次要问题(目前…):

  • 性能如何

  • p.S.I:不要注意示例的简单性。写它们只是为了理解我在传递ActiveRecords和params方法时的两难处境


    附言二:如果您需要其他信息,请告诉我。

    模块有两个主要用途:

  • 名称空间
  • 混血儿
  • 模块名称空间通常用于更好地组织代码,并促进更安全和一致的范围划分

    但模块主要用作混合模块。它是Ruby提供多重继承的方式。例如,假设您有需要跨类访问的方法(例如跨不同的模型/控制器等)。不要在每个类中重复那些不一定只适用于该类的方法,而是将这些方法抽象到一个模块中,并包括扩展相应类中的模块

    这取决于模块与应用程序目录的紧密耦合程度来决定 存储模块的位置。存储模块中的几种模式:

  • /lib目录,如果模块没有与app/特别“交互”
  • app/models目录,但可能会与实际的ActiveRecord模型混淆
  • 将其视为“关注点”,并将其存储在应用程序/关注点中 但是,如果您有一个只用于用户控制器的方法,建议您将该代码放入您的用户模型中,因为“用户”的业务逻辑应该在那里

    我假设“ActiveRecords”指的是模型类(比如User)。 您可以访问模型类并在模块中对其执行ActiveRecord操作(例如User.find(:all))


    但是,正如您猜对的,您不能使用params,您必须将其作为参数传递给模块的方法。

    关注点现在位于子目录
    app/controllers/concerns
    app/models/concerns