0

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

1 Answer 1

1

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.

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

1 Comment

Oh, so respond_with is what triggers the rendering of the view. That makes total sense now why anything set afterward wouldn't appear. I kind of figured something like that was the cause, but as I'm new to Rails I wanted to be sure what the exact cause was. Thanx so much for you explanation.

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.