Rails if else statement

Here i am going to give some example of conditional statements in Rails.
Normal IF ELSE
<% if user %>
<%= user.name %>
<% else %>
Anonymous
<% end %>

Normal code for ELSIF
if var == 10
print “Variable is 10″
elsif var == “20″
print “Variable is 20″
else
print “Variable is something else”
end

Technic:
How to Put this in one line:
<%= user.name if user %> This is the simplest way.

How to use IF and ELSE statement in one line:
<%= user ? user.name : “Anonymous” %>

Paperclip with Rails for image manipulation

I used the spree e-Commerce CMS for checking or R&D of Spree code. Spree is the really nice and basic tool for e-Commerce CMS.

When i was going through Spree i got to know about paperclip plugin which is used for image manipulation. Earlier i used “attachment_fu” for image manipulation and file uploading in rails projects.

How to use “Paperclip

First install perperclip plugin to your project.

Windows and Linux user can use my code. (I used this in WindowsXP and Fedora 9)

#ruby script /plugin install https://svn.thoughtbot.com/plugins/paperclip/trunk/

through this command perperclip get installed in your project folder.

Many time you need photo upload functionality for customer

Here i used Customer contoller and Customer model for this lession

First run following command

#ruby script/generate paperclip ModelName FieldName

In my case command is:

#ruby script/generate paperclip Customer customer_pic

One migration file will be created through this command. Run that migration.

In Customer model file paste this code:

class Customer < ActiveRecord::Base
# Paperclip
has_attached_file :customer_pic,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end

Using this command you will save three pic(photo) in your system folder.

Default upload url of peperclip is (RAILS_ROOT/public/system/…)

You can use following lines in your model file. (copy & paste this code under styles code)

:url => “/uploads/:class/:attachment/:id/:style_:basename.:extension”,
:path => “:rails_root/public/uploads/:class/:attachment/:id/:style_:basename.:extension”

I added uploads folder front on :class. That is optional you can remove also.

If you are already having forms for customer and if you want to add that to form. Just use this code in your form tag.

:html => { :multipart => true

and in from <%= f.file_field :customer_pic%>

That sit. you need to add or use following code in View

<%= image_tag @user.customer_pic.url %>

<%= image_tag @user.customer_pic.url(:thumb) %>

Paperclip Validations

Here i giving some validation for Paperclip. You need to just copy & paste in to your model where you want to use image upload.

validates_attachment_content_type :avatar, :content_type => 'image/jpeg'

validates_attachment_presence :avatar

I found this plugin is very usefull for Me. It really saves lots of time.

Fixed this issue: Building native extensions. This could take a while…

I got this error when i tried to install various ruby gems on system.

I am using the Fedoa 9 as OS.  I tried a lot to fix this issue and googled lot but not found exactly solution.

later on realize some of latest software are not installed on OS that’s why i am getting these kind of error.

you need to install proper software on Fedora box.

# yum install ruby ruby-libs ruby-mode ruby-rdoc ruby-irb ruby-ri ruby-docs

#yum install gcc-c++

this is the solution.


install netbeans 6.7 on fedora 9

For installation of  netbeans package you need JDK 1.6 installed on your Fedora box. on Fedora 9 you are having already installed JDK 1.6 so you need to update the installation only.

Use following command for installing or updating JDK installation.

#su

#ROOT_PASSWORD

#yum update java-1.6.0-openjdk-devel

#yum install java-1.6.0-openjdk-devel
Than go to this URL (http://www.netbeans.org/downloads/index.html) and download the .sh file which you want install on your Fedora 9 box.

As per my view Netbeans supports for Rails and PHP and Netbeans is best IDE for Rails and PHP.

For Linux platforms, the installer file has the .sh extension. For these platforms, you need to make the installer files executable by using the following command:
#chmod +x netbeans-6.7.0-ml-ruby-linux.sh

This command i given for ruby IDE, you can choose your IDE or as per file name.

Than run the installer file

#netbeans-6.5.1-ml-ruby-linux.sh

That sit netbeans installtion will start to PC. After installation you can start netbeans from programming menu.