开发者

ruby on rails function in helper?

I'm trying to make a function that takes 2 arguments from the u开发者_JAVA百科ser_controller and does a few queries and calculations and return the result in an array. Is this something I should set up in a helper file? And how would I return the results (I'm very new to this).

Thanks for all and any help :)


Unless those calculations are entirely related to your templates (html, js) you should NEVER put that stuff in helpers of any kind. It will make your app hard to test.

Your options: 1) Create a model without ActiveRecord (MyCalculations.rb in ˜/models or ~/lib) 2) Create an extension and include in existing models

Examples (sample code, not realistic) calculator.rb in ~/models

class Calculator

    attr_accessor :amount, :parcs, :interest, :change

    #... lots of code

  def initialize(amount, parcs, interest)
        # do stuff
        calculate
  end

    def self.calculate!(amount, parcs, interest)
        Calculator.new(amount, parcs, interest)
    end
end

Extensions: ~/lib/models/import/csv_ext.rb

module Models
    module Import
        module CsvExt
            extend ActiveSupport::Concern

            included do

            end

            module ClassMethods
                #static
                def load_from_csv(csv) 
                    # code comes here
                end


            end
            end
        end

end

Then, add it to your models:

include Models::Import::CsvExt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜