where to place code used by many views?

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.

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 :slight_smile:

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:

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

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.

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com