first Rails Aplication

September 9, 2008

Ror First application on rails 2.0
1. rails –database mysql hello
or rails -d mysql hello
2. create manually database on mysql
or rake db:create:all
3. ruby script/generate controller say hello

4. ruby script/server
5. type on http://localhost:3000/say/hello

for cearting table
script/generate scaffold Post title:string body:text
rake db:migrate

Steps
- rails -d mysql test
-cd test
—-manuially set the dtabase usernamr and pwd in database.yml file
-rake db:create:all
-ruby script/server (starting the server)
-ruby script/generate scaffold Movie title:string (for table cration)
-rake db:migrate

==for mongrel server
service mongrel_cluster restart
script/server –help
service nginx start

for including file
rendre:partial “path”;
render(:partial => ‘/controls/footer’)

for mongrel server steps

==start
1. copy mongrel_cluster.yml into config
changes that
2. insert some code in etc/ngix/*.conf
in that
upstream hello.rajesh {
server 127.0.0.1:8005;
}
and
server {
listen       81;
server_name  hello.rajesh;

root /sites/hello/public;

#charset koi8-r;

access_log  /var/log/nginx/hello.access.log  main;

location ~ ^/$ {
if (-f /index.html){
rewrite (.*) /index.html last;
}
proxy_pass  http://hello.rajesh;
}

location / {
if (!-f $request_filename.html) {
proxy_pass  http://hello.rajesh;
}
rewrite (.*) $1.html last;
}

location ~ .html {
root /sites/hello/public;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
root /sites/hello/public;
}

location / {
proxy_pass  http://hello.rajesh;
proxy_redirect     off;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

}

3. ln -s /sites/hello/config/mongrel_cluster.yml /etc/mongrel_cluster/hello.yml
4. chown -R mongrel:mongrel /sites/hello
==end
for creating only model and table
ruby script/generate model images_imagecategories image_id:integer imagecategories_id:integer
ruby script/generate model imagecategories_images images_id:integer imagecategories_id:integer


Document uplaoding and downloading instruction

September 3, 2008

#This is the sample appliocation code ,Please change as per your requirements.

==========For Document uploading============
1. model
-dcument.rb
def upload=(input_data)
if input_data != “”
self.filename = input_data.original_filename
self.documenttype = input_data.content_type.chomp
self.content = input_data.read
self.documentsize = input_data.size
end
end
2. controller
#display the doc /dopwnlaod
def showdoc# for download document
doc_id = params[:id]
#@doc = Document.find(doc_id)
#send_data(@doc.content, :type => @doc.documenttype,
#                 :filename => @doc.filename,
#                 :disposition => ‘inline’)
#
if doc_id != 0
begin
@doc = Document.find(doc_id)
if !(@doc.documentsize).blank?
send_data(@doc.content, :type => @doc.documenttype,
:filename => @doc.filename,
:disposition => ‘disposition’)
#increment download count
@count =(@doc.downloadcount)+1
@up = Document.update(doc_id, :downloadcount => @count)
#end incerement
return
end
rescue
end
end
flash[:notice] = “Error occured during downlaoding.”
redirect_to :action => “index”
end
#save document
def savedoc
if params[:doc][:title].blank?
flash[:notice] = “Title cannot be left blank.”
elsif  params[:doc][:upload].blank?
flash[:notice] = “Please select Document.”
else
@doc = Document.new(params[:doc])
if @doc.save
flash[:notice] = “Document was successfully created.”
redirect_to :action => “index”
return
else
flash[:notice] = “Error occured during Uploading.”
end
end
redirect_to :action => “adddoc”
end
#update document
def updatedoc
if params[:doc][:title].blank?
flash[:notice] = “Title cannot be left blank.”
else
@items = Document.find(params[:id])
if @items.update_attributes(params[:doc])
flash[:notice] = ‘Document was successfully updated.’
redirect_to :action => “index”
return
end
end
redirect_to :action => “editdoc”,:id=>params[:id]
end

3. view
-showdocument.rhtml
<% @items_all = Document.find(:all)
for @items in @items_all %>
<%=  link_to ‘Download’, :id => @items.id, :action => “showdoc” %>
<% end %>
-adddocument.rhtml
<%= form_tag ({:action => ’savedoc’},:multipart => true) %>
<table>
<tr><td>Title</td>
<td><%= text_field (“doc”,”title”,:size=>”30″) %><span id=”red”>&nbsp;*</span>
</td></tr>
<tr><td>Description</td>
<td><%= text_area (“doc”,”description”,:cols=>”28″,:rows=>4)%>
</td></tr>

<tr><td>Upload Document</td><td>
<%= file_field ‘doc’, ‘upload’, :size => “20″ %><span id=”red”>&nbsp;*</span>
</td></tr>
</tr>
<tr><td colspna=”2″>&nbsp;</td></tr>
<tr><td colspna=”2″><input type=”submit” name =”save” value=”Add”></td></tr>
</table>
</form>
4. database table structure
-document table
id=int
title=varchar
description=varchar
filename-varchar
documenttype=varrchar
documentsize-bigint(11)
content – longblob
downlaodcount-bigint(11)


Image uploading Instruction with sample

September 3, 2008

#This is the sample appliocation code ,Please change as per your requirements.

======Image Uploading instruction=========

1.routes.rb
#this is for image display
map.with_options :controller => ‘images’ do |image|
image.show ‘images/show/:size/:id/:default_image’, :action => ’show’, :id => 0, :default_image => nil,  :requirements => {:size => /(tiny|small|medium|original)/}
end
2. model
-image.rb file

require ‘RMagick’
class Image < ActiveRecord::Base
def thumb=(input_data)
if input_data != “”
self.name = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.image_original = input_data.read
img = Magick::Image.from_blob(self.image_original)
original_height = img[0].rows
original_width = img[0].columns
self.height = original_height
self.width = original_width
tiny_height = 32
tiny_width = 32
small_height = 85
small_width = 85
medium_height = 180
medium_width = 180
# resize the photo keeping aspect ratio
if original_height > original_width
tiny_width = original_width * tiny_height / original_height
small_width = original_width * small_height / original_height
medium_width = original_width * medium_height / original_height
else
tiny_height = original_height * tiny_width / original_width
small_height = original_height * small_width / original_width
medium_height = original_height * medium_width / original_width
end
self.image_tiny = img[0].thumbnail(tiny_width, tiny_height).to_blob
self.image_small = img[0].thumbnail(small_width, small_height).to_blob
self.image_medium = img[0].thumbnail(medium_width, medium_height).to_blob
end
end
end

3. controller
-images_controller.rb file
#this is for image display
def show
# verify valid size parameter here
valid_display_size = ["tiny", "small", "medium", "original"]
display_size = (valid_display_size.include?params[:size])?params[:size]:”medium”
#display_size = params[:size]?params[:size]:”medium”
image_id = params[:id]
default_image = params[:default_image]?params[:default_image]:”no_photo_#{display_size}”
if image_id != 0
begin
@image = Image.find(image_id)

if !(eval(“@image.image_#{display_size}”)).blank?
send_data(eval(“@image.image_#{display_size}”), :type => @image.content_type,
:filename => @image.name,
:disposition => ‘inline’)
return
end
rescue
end
#   # show default image here
#   #send_image(“#{RAILS_ROOT}/public/images/#{default_image}”)
#   @no_image = File.open(“#{RAILS_ROOT}/public/images/#{default_image}.png”,”r”)
#   send_data(@no_image.read, :type => “image/png”,
#              :filename => “#{default_image}.png”,
#              :disposition => ‘inline’)
end
#else
# show default image here
#send_image(“#{RAILS_ROOT}/public/images/#{default_image}”)
@no_image = File.open(“#{RAILS_ROOT}/public/images/#{default_image}.png”,”r”)
send_data(@no_image.read, :type => “image/png”,
:filename => “#{default_image}.png”,
:disposition => ‘inline’)
#end
end

#save iamge
def saveimage
if params[:image][:title].blank?
flash[:notice] = “Title cannot be left blank.”
elsif  params[:image][:thumb].blank?
flash[:notice] = “Please select Image.”
else
@picture = Image.new(params[:image])
if @picture.save
flash[:notice] = “Item was successfully created.”
redirect_to :action => “index”
return
else
flash[:notice] = “Error occured during insertion.”
end
end
redirect_to :action => “addimage”
end

#update iamge
def updateimage
if params[:image][:title].blank?
flash[:notice] = “Title cannot be left blank.”
else
@items = Image.find(params[:id])
if @items.update_attributes(params[:image])
flash[:notice] = ‘Photo was successfully updated.’
redirect_to :action => “index”
return
end
end
redirect_to :action => “editimage”,:id=>params[:id]
end
4. view
-add_image.rhtml=
<%= form_tag ({:action => ’saveimage’},:multipart => true) %>

<table>
<tr><td>Title</td>
<td><%= text_field (“image”,”title”,:size=>”30″) %><span id=”red”>&nbsp;*</span>
</td></tr>
<tr><td>Description</td>
<td><%= text_area (“image”,”description”,:cols=>”28″,:rows=>4)%>
</td></tr>
<tr><td>Upload Image</td><td>
<%= file_field ‘image’, ‘thumb’, :size => “20″ %><span id=”red”>&nbsp;*</span>
</td></tr>
</table></form>
#show image/display
<% @items_all = Image.find(:all)
for @items in @items_all %>
<%= image_tag(“/images/show/small/#{@items.id ? @items.id : “0″}/no_photo_#{“”}_small”, :alt => “Image not available”) %>
<%= image_tag(“/images/show/medium/#{@items.id ? @items.id : “0″}/no_photo_#{“”}_medium”, :alt => “Image not available”) %>
<% end %>
5. database structure
image table===
id=int
title=varchar
name=varchar
content_type=varchar
image_tiny=midiumblob
image_small=midiumblob
image_midium==midiumblob
image_original==midiumblob
height=bigint(11)
width=bigint(11)