0

at the top of my controller, outside of any method, I have

    @@javascript_is_disabled = false

and I have methods that that the view calls and invokes something like this

    @@javascript_is_disabled = params[:javascript_disabled]

but when I need the @@javascript_is_disabled in completely different method.. it is always false.

I know it changes in the method with params args... cause those methods behave differently, and appropriately

And ideas?

1 Answer 1

2

The variable @@javascript_is_disabled is a class variable and it refers to a different thing depending on where you access it from. From within the Controller class body it doesn't refer to the same thing as when you use it from within a controller method or a view. This is actually a pretty complex subject involving Eigenclasses

I suggest implementing it using a view helper or a protected method:

protected

attr_writer :javascript_is_disabled
def javascript_is_disabled
  # Replace false with your intended default value
  @javascript_is_disabled.nil? ? false : @javascript_is_disabled
end

Then you can reference it from within your views and controller action methods like an attribute javascript_is_disabled = true or if javascript_is_disabled ...

You could also leave out the attr_writer ... part and just always remember to assign values to the instance variable @javascript_is_disabled = ...

Sign up to request clarification or add additional context in comments.

Comments

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.