Showing posts with label hafeez. Show all posts
Showing posts with label hafeez. Show all posts

Wednesday, February 11, 2009

IMAGE UPLOADING

Image uploading is easy in ruby on rails by using attachment_fu plugin

To install attachment_fu
ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/


Then u need to design table in migration like

here we will take user images
class CreateUserImages < ActiveRecord::Migration
def self.up
create_table "user_images",:force => true do |t|
t.column "user_id", :integer
t.column "primary", :boolean
t.column "created_at", :datetime

# Required attributes for attachment_fu plugin
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
end

def self.down
drop_table :user_images
end
end


In model we need to customize validations

class UserImage < ActiveRecord::Base
belongs_to :user

has_attachment :content_type => :image,
:storage => :file_system,
:min_size => 0,
:max_size => 1.megabytes,
:resize_to => '640x480',
:thumbnails => { :medium => '200x200', :small => '80x80', :tiny => '40x40' }

# 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


And in User model

class User < ActiveRecord::Base
has_many :user_images


def primary_image
user_images.find_by_primary(true)
end
end


Here in controller only to display and create

class UserImagesController < ApplicationController
def new
@image = UserImage.new
respond_to do |format|
format.html
end
end

def create
# Required for Windows machines. Uncomment if you using Windows
# sleep(1)
user_data = {:user => current_user, :primary => current_user.images.empty? }

@image = UserImage.new(params[:image].merge(user_data))

respond_to do |format|
if @image.save
flash[:success] = "image successfully uploaded"
format.html { redirect_to(edit_user_path(current_user)) }
else
format.html { render :action => "new" }
end
end
end
end


And in new.rhtml

Current Primary Photo


<% if(@current_user.image.nil?) %>
<%= image_tag("default.png") %>
<% else %>
<%= image_tag(@current_user.primary_image.public_filename(:medium)) %>
<% end %>

Upload a new photo


<%= error_messages_for :image %>

<% form_for(:image, :url => user_images_path,
:html => { :multipart => true }) do |f| %>

Select a photo to upload

Photo: <%= f.file_field 'uploaded_data' %>
<%= submit_tag 'Upload Photo' %>
<% end %>


Follow the steps
Rake db:migrate
Then Script/server


If u want ur own validation to image then here are some key points
has_attachment(options = {})

* :size
o Range of sizes allowed.
o (1..1.megabyte) is the default. This overrides the :min_size and :max_size

* :resize_to
o Used by RMagick to resize images.
o Pass either an array of width/height, or a geometry string.

* :thumbnails
o Specifies a set of thumbnails to generate.
o This accepts a hash of filename suffixes and RMagick resizing options.
o This option need only be included if you want thumbnailing.
o :thumbnail_class
o Set which model class to use for thumbnails.o This current attachment class is used by default.

* :path_prefix
o path to store the uploaded files.
o path to store the uploaded files.
o Uses public/#{table_name} by default for the filesystem, and just #{table_name} for the S3 backend.
o Setting this sets the :storage to :file_system.

* :storage
o Specifies the storage system to use.
o Defaults to :db_file. Options are :file_system, :db_file, and :s3.

* :processor
o Sets the image processor to use for resizing of the attached image.
o Options include ImageScience, Rmagick, and MiniMagick. Default is whatever is installed.

* :content_type
o Allowed content types.
o Allows all by default. Use :image to allow all standard image types.

* :min_size
o Minimum size allowed.
o 1 byte is the default.

* :max_size
o Maximum size allowed.
o 1.megabyte is the default.



Reference link is: http://www.jumbabox.com/2008/06/attachment_fu-tutorial/

Thursday, November 20, 2008

Color Combinations

#669851 #950303 #FAFF02 #2E4F00
#3D5E31 #600202 #C4BF23 #78D700
#58679E #DEBB01 #140FDB #CE5401
#C9B68C #DEBB01 #08047D #813100
#975264 #232A3D #B13B00 #080F19
#6C3D47 #5D8DBD #FF5605 #314D7D
#252A3D #394F67

For more details visit
http://www.allwebdesignresources.com/colorschemescombinations.html

Friday, October 17, 2008

Solution For "rewrite" error

If you get this error, and the error message is pointing you to a “link_to” call or something similar, then you may be using an instance variable that’s called ‘@url’ too.

I finally discovered that, in my case, I was using ‘@url’ in the controller for the view where I was making the ‘link_to’ call.

Long story short, if you see this error, comb through your code (models, controllers and views) for any variables that are called ‘@url’ and change them.


Reason:

We should not use "url" word as controller name,model name,action name,attribute name ..
Apparently you can’t use “Class” as a class in Java either.

Yep, same as ‘private’, ‘public’, etc. With the slight difference that these are keywords at the language as oposed to framework level.


Solution:
Then simply replace url word with other..