Forum: Ruby on Rails Hiding HTML Markup

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B6c6d704b70db2f102b0f2eebc5c8e93?d=identicon&s=25 John Philp (philpj)
on 2006-05-29 16:57

THis may be a simple question. I am trying to hide some HTML markup
language depending on a value. So for example I have the following in an
RHTML file:

<%if session[:role]=="Administrator" %>
 <%= link_to("Users", :controller=>"Users",:action=>"list") %></br>

I want to be able to hide the Label part if the user is not an
Administrator and output it if the user is.

I dont know how to do it using Rails.
Could somebody give me an idea
91495a475b549e12f76404ed65321788?d=identicon&s=25 Guest (Guest)
on 2006-05-29 17:26
<% else %>
Bef7ff8a0537495a1876ffebdc9f8e51?d=identicon&s=25 Lionel Bouton (Guest)
on 2006-05-29 17:30
(Received via mailing list)
John Philp wrote the following on 29.05.2006 16:57 :

>I want to be able to hide the Label part if the user is not an
>Administrator and output it if the user is.

It should work as is (minus the missing % after "end" problem).

One remark, though :
you should not have a "session[:role]" if role is a user property.

You should probably code an accessor like the following, made available
through application.rb and application_helper.rb:

    def current_role
        current_user ? current_user.role : nil
    def current_user
        # Cache the User instance (probably used multiple times in a
        return @cached_user if @cached_user
        # The find (:first ...) instead of a "find(id)" avoids
exceptions when the id isn't in DB
        session[:user_id] ? (@cached_user = User.find(:first,
=> [ 'id = ?', session[:user_id]])) : nil

@cached_user avoids multiple calls to the DB. You end up 1/ restoring
the session, 2/ doing a single find for your user on each request
instead of only 1/ restoring the session.
Roughly the same amount of data is transferred but this is a little bit
slower than a single request. So why bother? You get a lot of robustness
for a very small perf penalty, see below.

Your login method should only put the logged-in user id in session.
Otherwise you'll get multiple problems:
- when restoring sessions you'll need various models pre-loaded when
restoring the user (depending on the objects stored in the session
through the user instance which can cache itself other objects), in
short all associated models must be loaded by a
model :<model1>, ..., :<modeln>
line in application.rb to avoid such problems.

- when changing the user model, you can't restore the sessions anymore
(objects dumped with old models can't be restored with the new one),
- changes applied to the logged in user or associated cached objects
aren't visible if done by using other instances of the same objects (in
other sessions but also in the same session...).

In short, you'll avoid a lot of trouble by neither storing more than the
smallest bit of information you need in session: the user id. In
practice, the perf penalty isn't even noticeable.

E6d130d35ecbda878f38318b98d5043c?d=identicon&s=25 Clayton Cottingham (Guest)
on 2006-05-29 21:12
(Received via mailing list)
Looks like your using the User Engine

If that's true they have a helper method that's most efficient:


{:controller=>'listing',:action=>'edit', :id => @listing } ) %>

Will only show the link if the current users is allowed to
This topic is locked and can not be replied to.