Solitary Coder
Solitary Coder

Reputation: 13

Accessing controller params inside of rails model method

I have params[:tab] within my activities controller. This is used to switch between different tabs on a view. I want to be able to access this param within my model method self.search_my_work.

Activities controller

if params[:tab].blank? || params[:tab] == 'active' || params[:tab] == 'inactive' || params[:tab] == 'overdue'

Activities model

    if tab == 'overdue'
   do this
    else
      do this        
   end

As it stands right now I get a Name error. I am aware it needs instantiated but I don't know how.

Upvotes: 0

Views: 334

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23711

You can't directly access controller params in your model and should not.

Solution:

Pass it as a param to the method

E.g:

# controllers/activities_controller.rb

Activity.results_for(params[:tab])

And use it

# models/acctivity.rb

def self.results_for(status)
  where(status: status)
end

Upvotes: 2

Related Questions