Ruby on Rails

attachment_fu file rename file rename / change filename : HOWTO


I’ve been looking for a solution to change the filenames of the uploaded files and came across the great tutorial at http://the.railsi.st/2007/8/21/how-to-customize-attachment_fu-file-names/ but it was not what i was exactly looking for. I had issues with viewing the saved files because the thumbnail property does not exist for the new attachment etc.

I’m posting what I did and it changes the filenames to what ever we specify.. Works for me, and hopefully it will work for someone else as well.. :)

checkout the pastie for a better view of the code http://pastie.caboo.se/161062

class Photo < ActiveRecord::Base
#Options for has_attachment
has_attachment	:content_type	=> :image,
:storage 	=> :file_system,
:size => 1.kilobyte .. 10.megabytes,
:resize_to => '500>',
:thumbnails => { :thumb => '125' },
:path_prefix => 'public/images/product_pics',
:processor    => 'MiniMagick'
validates_as_attachment
belongs_to  :product
#Filename changing stuff, these methods resides in attachement_fu plugin files..
#and we are just modifying them to fit our needs and putting them in our model
#so other models will not be effected
def full_filename(thumbnail = nil)
file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix]
@thumb = self.thumbnail unless (@thumb = thumbnail) #checks if thumbnails info already exist
File.join(RAILS_ROOT, file_system_path, self.product.code, thumbnail_name_for(@thumb, self.product.code))
end
#Change the filenames and thumbnail suffixes
def thumbnail_name_for(thumbnail = nil, asset = nil)
suffix = "_#{thumbnail}" unless thumbnail.blank?  #sets suffix by the name of the thumbnail set in has_attachment options
extension = filename.scan(/.w+$/) # extracts extension
return "#{asset}#{suffix}#{extension}" # change the filename to fit your needs
end
before_thumbnail_saved do |record, thumbnail|
thumbnail.product_id = record.product_id
end
end

Posted in Ruby / RailsComments (0)

Controller Inheritance and RESTful routes


Sometimes we might need to implement controller inheritance; to have a DRYer code, a logical way to organize our project using or let it be purely the personal preference. It would be especially helpful for an admin area of a website (which I wanted to implement as well as shown in the example of Justin French)

Admin Lounge Controller:

class Admin::ProductsController

This has been well explained here and it helped me to fix few things which I’ve been struggling to do and also convinced me i was doing something meaningful.

then if we need to use restful routing for these and if you dont know how to create the route similar to admin_products_url or edit_admin_product_url. routes.rb got an example how to get it done. However you can find a better example here.

Controller Inheritance
http://justinfrench.com/index.php?id=122

RESTful routes for Controller Inheritance
http://railstips.org/2007/4/28/namespaces-added-to-routes

Great RESTful RAILS tutorial
http://www.b-simple.de/download/restful_rails_en.pdf

Posted in Ruby / RailsComments (0)

RAILS on Ubuntu 7.1


Windows platform may not be the ideal place to develop or deploy your Apache/MySQL/PHP or RoR(Ruby on Rails) applications. However I came across few problems myself trying to setup Ruby on Rails Development Machina on Linux (Ubuntu 7.10). Once you fix them, it works like a charm

There are many installation instructions and fixes posted everywhere. I thought of putting my own version to consolidate installation instructions and fixes to possible problems. I’ve also listed the sources I’ve referred myself and its recommended visit those websites if you need more detailed instructions as well as up-to-date news.

POSSIBLE ISSUES

  • rubygems (LoadError) require’: no such file to load – rubygems (LoadError)
  • MySql socket error No such file or directory - /tmp/mysql.sock
  • Open ssl problem when starting Webrick server

1. Install libopenssl-ruby1.8 » fixes the Webrick server error on startup

2. Install libmysqlclient15-dev » fixes the MySQL sock not found issue
Reference:
https://help.ubuntu.com/community/RubyOnRails

3. Install Ruby and Gems
Even though you can easily get ruby installed using apt-get it doesn’t fix the issue. We’ll have to compile it ourselves.
Reference:
http://www.thirdbit.net/articles/2007/10/04/installing-ruby-186-on-ubuntu-feisty-fawn-704/
http://www.urbanpuddle.com/articles/2007/08/17/mongrel-cluster-1-0-2-and-ruby-1-8-5
http://www.urbanpuddle.com/articles/2008/01/09/install-ruby-on-rails-on-ubutu-gutsy-gibbon-apache-version

  1. Download ruby
  2. tar xjvf ruby-1.8.6.tar.bz2
  3. cd ruby-1.8.6
  4. sudo apt-get build-dep ruby1.8
  5. ./configure –prefix=/usr
  6. make
  7. sudo make install
  8. ruby -v and if you see something like ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux] ruby is installed :)
  9. then we install ruby gems. download rubygems and extract it.
  10. cd rubygems-1.0.1
  11. sudo ruby setup.rb

4. Install Rails
Reference:
http://rubyonrails.com/down
http://rubyonrails.com/

sudo gem install rails –include-dependencies

We are done.. Worked for me, and fingers crossed it’ll work for anyone as well :)

Posted in Linux, Ruby / RailsComments (2)

Good RAILS (Ruby on Rails) IDE for Windows


I’ve been trying to do a small e-commerce website using Ruby on Rails recently. I’ve being looking for a good IDE for RAILS that runs under windows. Everybody talks about textmate on apple, unfortunately (still) being a PC user and still stuck with windows (since adobe is not planning to release CS3 for Linux).

I’ve been evaluating almost every editor available. I’ve tried Aptana studio RadRails yet it seems to be too heavy if you are just a simple RAILS developer with a low end system, otherwise its a feature rich, cross-platform and actively developed.

I came across this cool program named RoRED. Light weight and easy to use. Sure may have lot of things to improve but with its code completing macros RoRED gets the job done well for me (so far). It’s not Open Source (but as of today it’s free) yet worth a look if you are looking for a lightweight and good IDE for RAILS.

Posted in Ruby / RailsComments (0)