# Where to place code used by many views?

**URL:** <https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996>\
**Category:** Rails\
**Created:** [February 18, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996 "2007-02-18T20:50:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/1 "2007-02-18T20:50:07Z")

</div>

Where should I put code (let’s call it a library function, perhaps)  
that is to be made available in several views, even views related to  
different models / controllers?

Is that what the “lib” directory is for? Or maybe the controllers/  
application.rb file?

And, how do I access this code later, in a given code? I’m guessing I  
have to do “require”.

I am wary of experimenting with such things in RoR, because I’d like  
to start following best practices, as viewed by the community.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/2 "2007-02-18T21:38:10Z")

</div>

Can you give an example of what you’re trying to accomplish?

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/3 "2007-02-18T22:14:12Z")

</div>

On Feb 18, 4:37 pm, “Jay Jansheski” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you give an example of what you’re trying to accomplish?

One example would be a navigation bar to be shown at the top of each  
page. I’ve done that a lot in PHP, using a subroutine that prints out  
text elements and links to go under the elements, highlighting one of  
these elements if it’s the present page. Thus, a lot of pages would  
call this function, e.g. the second in the list would call  
navbar(2)  
which means that when navbar() prints out it’s list, the second item  
should be highlighted to indicate that this is the present page. The  
pages need just this navbar() call; if I decide to add new pages (and  
thus new tabs), I only have to alter the shared code (this navbar()  
function), not the existing pages.

I’m sorry to say “PHP” in this post 🙂

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/4 "2007-02-18T22:47:15Z")

</div>

Jay, this is extraordinarily helpful, and generous. Many thanks. Dan.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/5 "2007-02-18T22:35:14Z")

</div>

For a sitewide navigation bar, you should do a shared partial.

In “views,” put a file called \_nav.rhtml (or whatever).

Then in your default layout, or wherever you might want to call it,  
you can put

\<%= render :partial=\>‘shared/nav’ %\>

The partial should pick up any instance variables (variables prefixed  
with ‘@’) you send to it, but if you want to define a variable  
specific to that partial, you can do it like this:

\<%= render :partial=\>‘shared/  
nav’, :locals=\>{:this\_section=\>‘articles’} %\>  
Which gives you access to a variable inside the partial called  
this\_section with the value ‘articles’.

If you’re talking about something specific to one of your models, you  
should put it in the view for that model. Let’s say you want to put  
user’s account info on every page. You might do something like this:

## VIEW:

## /views/user/\_account\_info.rhtml

Hi, \<%= @this\_user.name %\>!

## CONTROLLER:

## application.rb

:before\_filter :get\_this\_user

def get\_this\_user  
@this\_user = User.logged\_in\_user(session[:this\_user\_id])  
end

## MODEL:

## user.rb

def self.logged\_in\_user(id)  
find(id)  
end

Then anywhere in your application you can put:

\<%= render :partial=\>‘user/account\_info’ %\>

This may be too much info (or not enough) but I hope it helps.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/6 "2007-02-18T22:59:20Z")

</div>

I see a couple of sloppy errors in my example:

The shared partial should go in views/shared (not just views), and  
before\_filter should not have a colon in front of it.

Yes  
before\_filter :whatever

No  
:before\_filter :whatever

Anyway, best of luck.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 18, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/7 "2007-02-18T23:16:22Z")

</div>

I am trying to use the :click\_to\_edit\_text attribute for  
in\_place\_editor\_field, but nothing shows up. Any trick that I need to  
be able to pass in a default value for an in place editor?

Thanks!

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 19, 2007, 2:46am UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/8 "2007-02-19T02:46:37Z")

</div>

On Feb 18, 2007, at 5:14 PM, Nathan L. wrote:

> I am trying to use the :click\_to\_edit\_text attribute for  
> in\_place\_editor\_field, but nothing shows up. Any trick that I  
> need to  
> be able to pass in a default value for an in place editor?
> 
> Thanks!

Well, are you passing the :click\_to\_edit\_text in the right place?  
There are two hashes at the end of the argument list for  
in\_place\_editor\_field. If all you want to change is the hover text:

\<%= in\_place\_editor\_field ‘person’, ‘eye\_color’ {},  
{ :click\_to\_edit\_text =\> “Please click me!” } %\>

If you just have arguments like \<%= in\_place\_editor\_field ‘person’,  
‘eye\_color’, :click\_to\_edit\_text =\> “Please click me!” %\> the option  
will be part of the hash for the tag\_options rather than the  
in\_place\_editor\_options.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 20, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/where-to-place-code-used-by-many-views/85996/9 "2007-02-20T18:26:38Z")

</div>

dankelley wrote:

> Where should I put code (let’s call it a library function, perhaps)  
> that is to be made available in several views, even views related to  
> different models / controllers?
> 
> Is that what the “lib” directory is for? Or maybe the controllers/  
> application.rb file?
> 
> And, how do I access this code later, in a given code? I’m guessing I  
> have to do “require”.
> 
> I am wary of experimenting with such things in RoR, because I’d like  
> to start following best practices, as viewed by the community.

Use helpers, in the app/helpers directory) to define a common set of  
libraray functions.
