How do I make a method available to both my controller and model in Rails?

前端 未结 3 1995
面向向阳花
面向向阳花 2021-02-08 09:54

I have a private method in my Rails app to connect to Amazon S3, execute a passed block of code, then close the connection to S3. It looks like so;

def S3
  AWS:         


        
3条回答
  •  再見小時候
    2021-02-08 10:36

    You can write a module as :

    module MyModule
      def self.S3(args*)
        AWS::S3::Base.establish_connection!(
          :access_key_id     => 'Not telling',
          :secret_access_key => 'Really not telling'
        )
        data = yield
        AWS::S3::Base.disconnect
        data
      end
    end
    

    and then call it in your controller or model as

    MyModule.S3(params*)

提交回复
热议问题