Layout, application_helper and partial ! Help!

I am relatively new to RoR and I am trying to implement a simple content
management system. I know I am reinventing the wheel but you don’t learn
much using someone else’s code.

So anyway, my website has a main controller. I use a layout “main”
shared by all the controllers on the website (using the layout ‘main’
declaration in the controllers). In the layout, I use an
application_helper to display informations related to the user (if he
has messages, logged or not, etc…) which selects the right partials to
render (if not logged in -> display the log in form, if logged ->
display user infos)

The problem : RoR is looking for the partial files in the view directory
of the currently used controller. It’s fine as long as I am using the
main controller. But if for exemple the users wants to modify its
profile (which is hadnled by the user controller), there’s an error
stating that partials cannot be found in the controller views.

Is it a design flaw on my part or is there a way to have partials
availaible for all controllers so I can use them in a single layout for
my whole application ?

Thanks in advance !

Nicolas Jaccard

You can include the path to your partial when you render it:

render :partial => ‘user/my_partial1’
render :partial => ‘comment/my_partial2’

As for the way you’ve describe your app, it sounds like you’re
triggering a
lot of queries from your helpers. Personaly I like to keep that to a
minimum, especially for essential entities such as comments. It’s handy
see from just looking at your controller what data you’re providing.
small queries in a helper is fine, i.e count rows, getting user’s real
Don’t burry things away to quickly, you may know where they are now and
they’re doing, but 6 months after your site has gone live are you still
going to remember so readily?

thanks for your thoughts, as I said im new to this design philosophy !
My helper doesn’t actually trigger a query, as it uses an object stored
in the application_controller which contains the user informations if
he’s logged on (it uses session stored user_id to fetch the data)
Is it a good thing to do or not ?

And when you talk about burrying stuff away, how would you actually
display the user informations ? I first thought of just using the helper
without partials but doing view related stuff like formatting in an
helper didn’t seem right.

I really appreciate the help!

Nicolas Jaccard

Loading in the controller and providing it to the views is fine, that’s
I do. Using the stored user_id is fine yes. Remember to ensure that the
requested data actually applies to the logged in user though, and not a
different one.

Definitely use a partial for that, helpers are intended for providing
information, not HTML.

for example:

in the controller:

@user = User.find(params[:id]) # find sanitizes the id for you

if user_logged_in?(@user)

@profile = UserProfile.find(@user)


in the view:

<% if user_logged_in? %>
<%= render :partial => ‘user/profile’ %>
<% end %>

in the partial:

<%= get_friend_count(@user) %>

You could put the friend count query in the controller, but as your
get more complex and have a wider range of rendering possibilities, you
find you don’t always little things such as this, so it’s nice to put
in a helper. But make sure you don’t duplicate your queries when doing

DISCLAIMER: I’m fairly new to Rails myself, don’t please don’t take
everything I’ve said as fact!