Today I was writing code for displaying users on the webpage (in view file).
I have to display 7 users profile pic on home page of certain website. But the condition was as such:
First 4 profile pic will be top females (based on how frequently they visited site) and last 3 were male.
These pictures were to shown of the same user’s who are in the same country as in site visitor. For Example: If some one is accessing that site from USA thun it should show registered users from that country (from DB) only.
Login on this site was only using Facebook so for registered users we will use their current location to find the correct users.
Now I did write some code in controller to get 4 female users and 3 male users based on conditions and sorting criteria.
All these were to shown inside one UL tag only using LI tags. So I made 2 loops for each kind of users and thus I got those 7 users displayed.(There is only one table for both male and female users)
So far so simple; I guess.
Now real trick come in when I don’t have enough registered users from the visitor’s country. At that time I have to show some thing to users. I just can’t leave the image placeholders blank. So I wrote 1 loop below female user display and 1 more below male user display. The logic was if the count of previous loop is below 4 and 3 respectively then I will run the loop rest of the times and will show some predefined images in those place holder.
But my question is do I need to write these 4 loops in VIEW files as I wrote here or I should have wrote the same logic in Controller and stored combined results in one array and then display it on view page.
My Question is which is better in terms of speed and efficiency?
Writing such logic in presentation reduce the page render speed?
Writing code in controller support better code maintenance and clear separation?
As we know in Rails MVC controller is executed first what difference does that make in putting code in controller or view file.
Will post some code soon to explain my point more.
This view file shows the logic that I wrote for showing those users
<pre><ul> <!-- Display pics of users here --> <!-- First 4 are female users. If there are less than 4 users that match criteria then display default pic --> <% @users_female.each do |user| %> <li><img src="<%= user.profile_picture %>" width="50" height="50" /></li> <% end %> <% if @users_female.count < 4 %> <% for i in (@users_female.count+1)..4 %> <li><img src="images/female.png" width="50" height="50" /></li> <% end %> <% end %> <!-- Rest 3 are male users. If there are less than 4 users that match criteria then display default pic --> <% @users_male.each do |user| %> <li><img src="<%= user.profile_picture %>" width="50" height="50" /></li> <% end %> <% if @users_male.count < 3 %> <% for i in (@users_male.count+1)..3 %> <li><img src="images/male.png" width="50" height="50" /></li> <% end %> <% end %> </ul></pre>
@users_male and @users_female returns exactly 3 and 4 users from controller based on conditions.