# Access to an ApplicationController property from a view

**URL:** <https://www.ruby-forum.com/t/access-to-an-applicationcontroller-property-from-a-view/60957>\
**Category:** Rails\
**Created:** [May 18, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/access-to-an-applicationcontroller-property-from-a-view/60957 "2006-05-18T22:13:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [May 18, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/access-to-an-applicationcontroller-property-from-a-view/60957/1 "2006-05-18T22:13:38Z")

</div>

Hi there,

How is it possible to get access to an ApplicationController property  
from a view?

Consider this (simplified) situation:

class ApplicationController \< ActionController::Base  
def folder\_id  
@folder\_id = params[:id]  
return @folder\_id  
end  
end

class FolderController \< ApplicationController  
def show  
@folder = Folder.find\_by\_id(folder\_id)  
end  
end

So far, so good, BUT… when trying to access folder\_id in the show-view  
like this \<%= folder\_id %\>, I get an error (undefined local variable or  
method `folder\_id’). Is this possible or do I NEED to use instance  
variables – which I’d rather not do?

Thanks.

Mischa.

–  
Open source file management webapp :: Boxroom ::  
[http://boxroom.rubyforge.org/](http://boxroom.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [May 18, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/access-to-an-applicationcontroller-property-from-a-view/60957/2 "2006-05-18T22:42:55Z")

</div>

Hi Mischa,

Mischa B. wrote:

> So far, so good, BUT… when trying to access folder\_id in the show-view  
> like this \<%= folder\_id %\>, I get an error (undefined local variable or  
> method `folder\_id’). Is this possible or do I NEED to use instance  
> variables – which I’d rather not do?

You’ll need to use instance variables. By definition, the scope of a  
local  
variable is the block within which it’s declared. A local variable  
declared  
in a controller action method only has meaning within that action  
method.  
Not only is it not available in a view, it’s not available in any of the  
other action methods in the controller.

Just as a point of information, what’s your issue with using instance  
variables?

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [May 18, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/access-to-an-applicationcontroller-property-from-a-view/60957/3 "2006-05-18T22:56:14Z")

</div>

> You’ll need to use instance variables. By definition, the scope of a  
> local variable is the block within which it’s declared. A local  
> variable declared in a controller action method only has meaning within  
> that action method. Not only is it not available in a view, it’s not  
> available in any of the other action methods in the controller.

Yeah, but I’m not trying to use a local variable, I’m trying to access  
the method folder\_id which returns @folder\_id.

Why IS it possible to use params[:id] instead of @params[:id] in a view  
and why ISN’T it possible to use folder\_id instead of @folder\_id like  
I’m trying to do in my example?

> Just as a point of information, what’s your issue with using instance  
> variables?

I’m trying to encapsulate @folder\_id. The view only has to know about  
folder\_id. In the method I can set @folder\_id to different values  
according to the situation and return the appropriate value.

> Best regards,  
> Bill

Thanks again.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [May 18, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/access-to-an-applicationcontroller-property-from-a-view/60957/4 "2006-05-18T23:05:52Z")

</div>

On Thu, May 18, 2006 at 10:11:29PM +0200, Mischa B. wrote:  
} Hi there,  
}  
} How is it possible to get access to an ApplicationController property  
} from a view?  
}  
} Consider this (simplified) situation:  
}  
} class ApplicationController \< ActionController::Base  
} def folder\_id  
} @folder\_id = params[:id]  
} return @folder\_id  
} end  
} end  
}  
} class FolderController \< ApplicationController  
} def show  
} @folder = Folder.find\_by\_id(folder\_id)  
} end  
} end  
}  
} So far, so good, BUT… when trying to access folder\_id in the  
show-view  
} like this \<%= folder\_id %\>, I get an error (undefined local variable  
or  
} method `folder\_id’). Is this possible or do I NEED to use instance  
} variables – which I’d rather not do?

\<%= controller.folder\_id %\>

} Thanks.  
} Mischa.  
–Greg
