Forum: Ruby on Rails where to place code used by many views?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
dankelley (Guest)
on 2007-02-18 21:50
(Received via mailing list)
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.
Jay Jansheski (Guest)
on 2007-02-18 22:38
(Received via mailing list)
Can you give an example of what you're trying to accomplish?
dankelley (Guest)
on 2007-02-18 23:14
(Received via mailing list)
On Feb 18, 4:37 pm, "Jay Jansheski" <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 :-)
Jay Jansheski (Guest)
on 2007-02-18 23:35
(Received via mailing list)
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.
dankelley (Guest)
on 2007-02-18 23:47
(Received via mailing list)
Jay, this is extraordinarily helpful, and generous.  Many thanks.  Dan.
Jay Jansheski (Guest)
on 2007-02-18 23:59
(Received via mailing list)
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.
Nathan L. (Guest)
on 2007-02-19 00:16
(Received via mailing list)
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!
Rob B. (Guest)
on 2007-02-19 03:46
(Received via mailing list)
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
removed_email_address@domain.invalid
Deepak Vohra (Guest)
on 2007-02-20 19:26
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.
This topic is locked and can not be replied to.