Here's a sample code to assign a dynamic div for displaying companies, the ids should be assigned based on whether the user is logged in or not:
-
@companies.each
do |company|
-
if user_logged_in?
#sponsor_for_logged_users
.icon
= link_to company do
= image_tag company.file
%span{:class
=> 'classic'}
%p.name= company.name
%p= company.short_description
.benefits= company.benefits
-
else
#sponsor_for_unlogged_users
.icon
= link_to company do
= image_tag company.file
%span{:class
=> 'classic'}
%p.name= company.name
%p= company.short_description
.benefits= company.benefits
Too much repeatation :(
We
can't write:
-
@companies.each
do |company|
-
if user_logged_in?
#sponsor_for_logged_users
-
else
#sponsor_for_unlogged_users
.icon
= link_to company do
= image_tag company.file
%span{:class
=> 'classic'}
%p.name= company.name
%p=
company.short_description
.benefits= company.benefits
Because
HAML will take the yellow part as part of else.
So
what to do ???
Inside
controller's action let's decide which div to select
def
index
@div_class_sponsors
=
current_user.nil?
?
"sponsors_benefits_unlogged"
: "sponsors_benefits_logged"
end
The
view page above now becomes:
-
@companies.each
do |company|
%div{:id
=> @div_class_sponsors}
.icon
= link_to
company do
=
image_tag company.file
%span{:class
=> 'classic'}
%p.name=
company.name
%p=
company.short_description
.benefits=
company.benefits
Very
simple. Just an idea that came to my mind instead of using the ideas
available on other resources.
.
and # are used to represent div class and id resp.
But
we can always use %div.