Rails Import Class Method from lib -


i can't seem figure out why not work.

i have class in lib code:

    class sortmethods   def initialize(direction, sort)     @mydirection = direction     @mysort = sort   end   def sort_column(table, field)     table.column_names.include?(@mysort) ? @mysort : field   end    def sort_direction     %w[asc desc].include?(@mydirection) ? @mydirection : "asc"   end end 

in trucks_controller have code:

    class truckscontroller < applicationcontroller   # /trucks   # /trucks.json   require 'sort_methods'   helper_method :sort_column, :sort_direction   def index     search = params[:search]     msm = sortmethods.new(params[:direction], params[:sort])     @trucks = truck.search(search).order(msm.sort_column(truck, "truck_no") + " " + msm.sort_direction)      respond_to |format|       format.html # index.html.erb       format.json { render json: @trucks }     end   end end 

what don't understand doing wrong, i've tried suggested in post, person had same question yet not works. doing wrong?

post: rails - how call methods lib directory?

i should add tried adding .self , still not works.

look @ error. undefined local variable or method 'params' #<sortmethods:0x2224008>

that means sortmethods trying call params[].

only controllers can have access params object.

in sortmethods class, make sure not calling params object. if need parameter web, pass in argument 1 of methods. work.

class sortmethods   def initialize(param)     @param = param   end    def sort_column(table, field)     puts @param   end    def sort_direction     #some action   end end 

then in controller,

def index   msm = sortmethods.new(params[:something])   ... end 

conceptually pass entire params object, wouldn't want because should take need.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -