Hello,
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 ?
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
to
see from just looking at your controller what data you’re providing.
Sure,
small queries in a helper is fine, i.e count rows, getting user’s real
name
etc.
Don’t burry things away to quickly, you may know where they are now and
what
they’re doing, but 6 months after your site has gone live are you still
going to remember so readily?
Hello,
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.
Loading in the controller and providing it to the views is fine, that’s
what
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)
end
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
actions
get more complex and have a wider range of rendering possibilities, you
may
find you don’t always little things such as this, so it’s nice to put
them
in a helper. But make sure you don’t duplicate your queries when doing
this!
DISCLAIMER: I’m fairly new to Rails myself, don’t please don’t take
everything I’ve said as fact!
Ian.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.