0

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.

1
  • 1
    Pass it from the controller explicitly. Commented Oct 20, 2022 at 13:28

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Is there anything else I still need to do? I still get the same name error.
@SolitaryCoder: then you must have implemented this advice incorrectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.