In this tutorial I create an application using Google map API. I develop a map for a city and user can insert there location to the map and they can view those places by clicking the icon. When adding location they can add a photo of the location as well. This is very simple application and can develop it further more as you wish. For upload images I use attachment_fu plug-in and it can easily use for file upload purposes.
First create a rails application called Map.You can create application through Netbeans or in command line (windows). I am using the command line for creating the application.
C:\> rails Map
Now create the database called map_development. You can use postgres or mysql for create the database. I am using postgres for create the database. According to your database you have to edit the database.yml file. If its mysql replace the adapter “postgresql” to “mysql” give the database name, user name and password.
Development:
adapter: postgresql
database: map_development
username: postgres
password:
host: localhost
Then go to map folder and Run the rails application.
C:\> cd Map
C:\Map>ruby script/sever
Go to web browser type http://localhost:3000/ . If database is correct it should show the application’s environment without error.
Let install the Rick Olson's attachment_fu plug-in to the map application. You can get more information about the plugin using following link http://clarkware.com/cgi/blosxom/2007/02/24. This plug in require rails 1.2 or above rails version.
C:\Map> ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
Now we create a two model for map application. One is used with attachement_fu plug-in. This will store the uploaded information of the image file. Other model is use to store the location that insert by the user. First generate the image model for upload images.
C:\Map>ruby script/generate model image
Go to db/migrate/20090619033949_create_images.rb file insert the following code.
class CreateImages < ActiveRecord::Migration
def self.up
create_table :images do |t|
t.column "location_id", :integer
t.column "filename", :string, :limit => 255
t.column "path", :string, :limit => 255
t.column "content_type", :string
t.column "size", :integer
t.column "width", :integer
t.column "height", :integer
t.column "parent_id", :integer
t.column "thumbnail", :string
t.column "created_at", :datetime
t.timestamps
end
end
def self.down
drop_table :images
end
end
Let us create another model called location to store the user entered places. The fields are name, longitude, latitude and description. Longitude and the latitude is use for show the location in the map.
C:\Map>ruby script/generate model location
Go to db/migrate/20090619035403_create_locations.rb file and insert the following lines.
class CreateLocations < activerecord::migration
def self.up
create_table :locations do |t|
t.column "name", :string, :limit => 100
t.column "description", :string
t.column "longitude", :decimal
t.column "latitude", :decimal
t.timestamps
end
end
def self.down
drop_table :locations
end
end
Now you have created the required migration files, its time to execute them against the database. To do this, go to a command prompt and go to the library directory, in which the application is located, and then type rake migrate.
C:\Map>rake db:migrate
This will create images and locations tables in map_development database. Now go to database and check the two tables.
When uploading the images we have to validate those image for weather its file type and file size. Also we can resize the image when uploading. For this function you have to install one of the gem from following gems ImageScience, RMagick or minimagick. Otherwise this resizing function will not work.
Go to \app\models\image.rb and write following code.
class Image < activerecord::base
has_attachment :content_type => :image,
:storage => :file_system,
:min_size => 0,
:max_size => 1.megabytes,
:resize_to => '320x240',
:thumbnails => { :medium => '150x150'}
#Override the default AttachmentFu error messages.
def validate
if filename.nil?
#errors.add_to_base("You must choose a file to upload")
else
# Images should only be GIF, JPEG, or PNG
enum = attachment_options[:content_type]
unless enum.nil? || enum.include?(send(:content_type))
errors.add_to_base("You can only upload images (GIF, JPEG, or PNG)")
end
# Images should be less than UPLOAD_LIMIT MB.
enum = attachment_options[:size]
unless enum.nil? || enum.include?(send(:size))
msg = "Images should be smaller than 1 MB"
errors.add_to_base(msg)
end
end
end
end
We finished the model part. Now we create the controller part next.
To be Continue.....
No comments:
Post a Comment