Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert how to write in model...

Replytotopic

how to write in model

Posted in Forums : Ask a Rails expert

 
Warrior

Authority 12
Posting Rating 3
Sign in to rate this post

hi i am a beginner for rails how to write this in a model and access in a controller
def upload
@image=Image.new
@image.filename=params:image.original_filename
@image.content_type=params:image.content_type
@image.siz=params:image.size
@image.user_id=params:image
@image.save
end

 
Me

Authority 62
Posting Rating 100
Sign in to rate this post

Use a plugin like attachment_fu or Paperclip.

I prefer attachment_fu if a model has an undetermined number of attachments (e.g. a photo album has many photos) and Paperclip if I know that there’s only a certain number of attachments (e.g. a user has one avatar).

 
100_5106-785540

Authority 25
Posting Rating 0
Sign in to rate this post

I wrote a small step by step tutorial on setting up attachment_fu with image science:
http://blog.springenwerk.com/2008/05/setting-up-attachmentfu-for-rails-with.html

The post also has a link to a free chapter from the advanced rails recipes book which will answer your attachment_fu questions for sure.

- Johannes

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

You can use file_column plugin.

nstalling is pretty straightfoward, just like the majority of Rails plugins:

script/plugin install http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk

The first thing we need is to tell our model which column to use:

class Blog < ActiveRecord::Base file_column :image
end

Now, if we want to include a upload mechanism in our form, we need to include some code like this:

<%= file_column_field “blog”, “image” %>

<%= image_tag url_for_file_column(“blog”, “image”) %>

We can extend things further, like for instance, I may want to upload only “jpg” and “gif” images and I want them to have a certain size:

class Blog < ActiveRecord::Base file_column :image

validates_file_format_of :image, :in => ["gif", "jpg"]
validates_filesize_of :image, :in => 1.kilobytes..5000.kilobytes
end

Using R-magick for resizing of images

class Blog < ActiveRecord::Base file_column :image,

:store_dir => “public/images/my_stupid_place_to_keep_images_uploaded”, :magick => {:versions => { :thumb => {:crop => “1:1”, :size => “86×87!”, :name => “peq”}, :normal => {:crop => “1:1”, :size => “289×258!”, :name=>”med”} } }
end

 
Profile

Authority 62
Posting Rating 95
Sign in to rate this post

If all you need is file upload, you can do that in your model. http://gist.github.com/9323

If you need image magic stuff, go with paperclip, attachment fu etc.

Replytotopic

Other Recent Topics

Ask a Rails expert : json gem error

Ask a Rails expert : Problem with break

Ask a Rails expert : activesupport string first method error

Ask a Rails expert : url_for broken ?

Ask a Rails expert : map.routes.rb pls help

Ask a Rails expert : how to h tag in controller

Ask a Rails expert : Problem with Restful routing and partial form

Ask a Rails expert : will_paginate, search and ajax

Ask a Rails expert : captcha

Ask a Rails expert : Mass Deletes in Admin Section

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel