April 6, 2013
Remove backup file created by gEdit (files ending with ‘~’ sign)

I have a project on github which I develop on Ubuntu. I used gedit for a while to write all my codes (I am currently using Sublime Text2) and it create backup files for each file I work on. And when I commit my projects on Github, this back up files are also uploaded to github.

It was fine for a while, but in one of my interview I have been asked the reason why there are two copies of same file and It was very embarrassing to explain why I haven’t removed duplicate files. Yes I was lazy to remove it but this interview opened my eyes and I removed all my duplicate files using following command.

find -type f -iname '*~' -exec rm -rf {} \;

This command will remove all the files ending with ‘~’ sign in the directory you are running command and all subdirectory under it.

I found this command from following two links.

June 6, 2012
Uploading Rails 3 site on a server with Cpanel

Hi Last week I upload Rails 3.0.7 site on server. Provider of server had CentOS 5.8 (Final) and Cpanel and WHM 11.32.2.

It took me more than week to successfully upload site. I would like share that pain staking process with you.

First of all I was provided with root user name and password and Web Host Manager (WHM) root login.

First of Cpanel 11.32.2 doesn’t support Rails 3 at all. So I know at that time entire process has to be managed from command line only.


Last difficulty I faced was with image upload. Entire site was working perfectly but the form where user adds image was not working. First I thought it might have something to do with folder permission. I set the permission of system folder to 755 and then 777 and eventually going upward at the end entire project permission was 777 but no change in error. Then I checked the logs and I realized it was imagemagic that need to be installed.

Now I tries to install imagemagic through different ways as per following links. 1. sudo apt-get install libmagickcore-dev libmagickwand-dev - Failed even using yum because of cpanel 2. Following steps in this script http://stackoverflow.com/questions/6687312/how-to-install-imagemagick-for-use-with-rvm 3. But finally

May 3, 2012
"

Little Update to my first “File Upload in Rails” blog. File multiple = “multiple” doesn’t works with any version of IE as of today.(checked upto IE9).

To all multiple file you would need Flash. Once nice plugin for this is uploadify, Now they provide Flash and HTML5 version. (Again my guess is HTML5 is not compatible with IE9)

"

April 23, 2012
Which is Better? Writing presentation logic in View or in Controller in Rails

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:

  1. First 4 profile pic will be top females (based on how frequently they visited site) and last 3 were male.

  2. 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.

  3. 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.

View File

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.

April 11, 2012
How to upload multiple files in Ruby on Rails (No JS required)

Today I was working on uploading multiple images in one of my Rails project. I would like to share what I did.

I was using Rails 3.0.10 with carrierwave although I am pretty sure it can be used with paperclip and may be other gems like dragonfly.

Here is my view file:

<% form_for @screenshot,:url=>{:action=>"create"},:html => {:multipart => true}  do |f| %>
  <input type="hidden" id ="project_id" name="project_id" value="<%=params[:project_id]%>" >

  <div class = "field"> 
    <%= f.label :screenshot %><br />
    <%= f.file_field :screenshot, :multiple => true %>  
  </div>

  <div class="action">
    <%= f.submit "Upload"%>
  </div> 
<% end %>

My Model is as follow:

# Project Screenshot Model
class ProjectScreenshot < ActiveRecord::Base
  belongs_to :project_master
  mount_uploader :screenshot, ScreenshotUploader
end 

Upto this point everything was pretty self explanatory and simple. And here goes the most important part. Controller

def create
  params[:screenshot_length] = params[:project_screenshot][:screenshot].length
  params[:project_screenshot_screenshot] = params[:project_screenshot][:screenshot]
  params[:project_screenshot_screenshot_0] = params[:project_screenshot][:screenshot][0]
  params[:project_screenshot_screenshot_1] = params[:project_screenshot][:screenshot][1]

  @screenshots = Array.new
  i = 0
params[:project_screenshot][:screenshot].each do |screenshot|
    @screenshots[i] = ProjectScreenshot.new
    @screenshots[i].screenshot = screenshot
    @screenshots[i].project_master_id = params[:project_id]
    i = i + 1
end
screenshot_flag = false
@screenshots.each do |screenshot|
    if screenshot.save
        screenshot_flag = true
    else
        screenshot_flag = false
    end
end
  if screenshot_flag
    flash[:notice]="Successfully Added Screenshot"
    redirect_to :back
  else
    flash[:notice]="Some problem in screenshot. Not Saved."
    redirect_to :back
  end
end

As you can see in the first five line of controller; I tried to figure out how do I get parameter from form to my controller.

Once I knew that images are passed in the parameter in an array of params[:project_screenshot][:screenshot], rest of the story was very simple. I created an array and looped through param array, created an object of ProjectScreenshot (model name in my case) and saved them.

I spent a quite a bit of hours to figure out how to upload multiple images in Rails but finally It seems pretty easy to me. I decided to write a blog about this because I didn’t find any decent solution on the internet about this.

My next goal is to upload the same images with progress bar using jQuery File Upload or uploadify. My personalĀ favoriteĀ is first one because of it’s use of html5 and lack of flash.

Liked posts on Tumblr: More liked posts »