Search code examples
ruby-on-railsviewinstance-variables

Instance variable nil when in certain location


I'm trying to use the value of a controller instance variable in a view. The value of the variable will change depending on the value of a session variable. I was testing it out and I noticed that if I set the variable in a specific location it always comes up as nil.

I'm using Devise for authentication and every time I set the variable after the last Devise function it comes up as nil. But when I place it any where else it shows up fine. I just think that's weird and would like to know why it happens. Everything else works fine except for me setting the instance variable which is completely separate from Devise authentication.

@prop is the variable I'm trying to set up. If I set it like this, when I try to get to references the value in the view it shows up as nil

def new
self.resource = resource_class.new(sign_in_params)      
clean_up_passwords(resource)        
yield resource if block_given?      
respond_with(resource, serialize_options(resource))
@prop = "test value"
end

But when I set it like this or any other line before the respond_with I get the correct value in the view.

def new
@prop = "test value"
self.resource = resource_class.new(sign_in_params)      
clean_up_passwords(resource)        
yield resource if block_given?      
respond_with(resource, serialize_options(resource))
end

Thanx


Solution

  • respond_with is triggering the responder and it makes view rendered. As you put another variable after the respond_with, the view won't recognize it.