Tag Archives: ror

rails

Install Ruby 1.9.2 and Rails 3.1.0 RC on Mac OS X Snow Leopard

Hi Rubyists,

As I’m going to start a new project using Ruby on Rails and I just found Rails just released a new version as 3.1.0 Release Candidate 4 and will be out as an official release in a few weeks if everything goes well.

Well, I’m going to use my Mac mini as the primary machine to work on this project as it is a hobby project. (anyway, I have a strategy to make money for me as well)

So, the problem is Mac OS X snow leopard already bundled with Ruby 1.8.7 and we need to upgrade it to version 1.9.2.

First, we need to download the source code of Ruby 1.9.2, compile, install and install Rails 3.

Here is detailed instructions.

You need to install readline.


curl -O ftp://ftp.cwru.edu/pub/bash/readline-6.0.tar.gz
tar -xvzf readline-6.0.tar.gz
cd readline-6.0
./configure --prefix=/usr/local
make
sudo make install

Don’t concern on some warning, just make sure there is no error.

Next, you need to download Ruby 1.9.2 from http://www.ruby-lang.org/en/downloads/

Extract the archive, open a terminal and go to the extracted directory.

Then, execute these lines.


./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
sudo make install-doc

Then, you need to add this line into your “~/.bash_profile” file. (create a new one if it doesn’t exists)


export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

Close the terminal, open the new one, then check Ruby version using this command.


ruby -v

You will see it is now version 1.9.2!

Bingo, now just install RubyGems, or if installed, execute this command.


gem install rails --pre

–pre state that you want to install a pre-release version which is RC.

So, if Rails 3.1.0 is already an official version, you can ignore –pre parameter.

Hope this help.

Symfony

A Thing to Concern That Made Me Stay Away from Ruby on Rails of My Recent Project…

Even though, I found Ruby on Rails is a very powerful web application framework.

I really love its capabilities which made me able to…

  • Produce works with less code, much less than any competitive frameworks (Symfony, CodeIgniter, Zend Framework)
  • Fun to code
  • Reuse many interesting Gems
  • etc…

However, when I’m dealing with the most recent of my project, I found Ruby on Rails doesn’t fit for my project for sure.

And the only main reason made me switch from Ruby on Rails to Symfony is…

  • Ruby on Rails is so complex to deploy on generic hosting companies. (unless you can afford to use a premium Ruby on Rails specific hosting like Heroku or EngineYard)

Yeah, my recent project will use shorter time to code if I move on Ruby on Rails, however, I can’t accept with the difficulties of deployment because this project will be sold to mass users which doesn’t have enough technical knowledge to deploy Ruby on Rails application easily.

They do not know what is SSH, Nginx, Passenger and so on…

Also, I can’t force customers to use Heroku, even I can, my customers still have to face a difficult time to generate a private/public key using SSH as well.

So, I decide to go back and kick off the project using Symfony which is based from PHP.

Symfony

My point to write here is that each frameworks today can’t be compared, for me, it depends on how the framework fit for my post development strategy.

Yes, I’m using marketing-drive approach.

I’m not saying Symfony is better than Ruby on Rails or any other frameworks.

But it is very fit for my project’s strategy.

If today I have to develop an application to work as SaaS or it is hosted at my server only, I will probably choose Ruby on Rails 70-80%.

Because it is fun and less code for me.

rails

File Upload Handling with Ruby on Rails 3 using Paperclip

If you are finding a solution to implement file upload or file attachment, you almost landed to the right place.

Here I’m going to share with you how good paperclip is and why I think this is the best solution I found after working on web application development for over 7 years on almost every web platform.

After I’ve learn the basic of Ruby on Rails, I’m finding for a solution on file upload, even though it can be done through a simple HTML tag, it is not scalable.

How is your project if you are planning to move your web application into the world of Cloud?

With the common HTML file upload tag, you can’t scale and move your web application into cloud easily.

The paperclip created by thoughtbot, the creator of many favorite Gems.

thoughtbot

A short features of paperclip are…

  • It handling MIME type, file size, file name into your existing model
  • It can automagically generates a different version of original image with different size
  • You can move your web application into cloud environment immediately
  • Working with generic file storage, Amazon S3, Rackspace Cloud Files or Heroku

Just the above features, I’ve been convinced to look at it over any other Gems such as attachment_fu which I believe it also good as well.

What your only requirement to use paperclip is only the ImageMagick which available on every platform, I’ve set the things up and paperclip working in 10 minutes!

ImageMagick

Here is the quick steps you can follow to make the use of paperclip from you Rails application

  • gem install paperclip

    or using the Bundler by adding this line into your Gemfile

    gem 'paperclip'
  • Install ImageMagick, download it from http://www.imagemagick.org
  • Make sure you have the ImageMagick directory in your environment PATH variable
  • Edit your model by adding this line into it
    has_attached_file :banner

    (change ‘banner’ with your desired members name – This will tell paperclip to add file uploading into this model)

  • Generate new migration and add 4 fields into the table, the code will look like this…
  • class AddImagesToTable < ActiveRecord::Migration
    def self.up
    add_column : offers, :banner_file_name, :string
    add_column : offers, :banner_content_type, :string
    add_column : offers, :banner_file_size, :integer
    add_column : offers, :banner_updated_at,   :datetime
    end 
    
    def self.down
    remove_column : offers, :banner_file_name
    remove_column : offers, :banner_content_type
    remove_column : offers, :banner_file_size
    remove_column : offers, :banner_updated_at
    end
    end
  • Migrate the database!
    rake db:migrate
  • Modify the form tag, add multipart attribute…
    <%= form_for(@post, :html => {:multipart => true}) do |f| %>
  • Place the file upload object inside the form tag
    <%= f.file_field :banner %>
  • Now you can run and upload the file into the model!
  • If you want to render the file, use this piece of code
    <%= image_tag @post.banner.url %>

That’s all the basic sample how to use paperclip, it is surprisingly easy for me.

If you are looking for advanced features of paperclip, visit the project page at Github and you will get everything.

Hope this help.

20110512-g51wgm9m1mhk8c5yybb97qesbp

Am I Moved to Open Source World of Application Development?

It is weird to say, but after I’ve employed myself and coding various projects.

I found that I almost moved my resources from what all based on Microsoft technology to the world of open source.

And I found the productivity level still acceptable with a good selection of technologies.

I found the open source application development didn’t as bad as I think from the previous.

Even I’ve many high level of certification by Microsoft on application design, development. (MCPD, MCTS, MCSD, MCAD and so on…) I still quite happy to say that I’m happy entering into the world of open source today.

As my most projects are built as a web application, I mainly moved from ASP.NET MVC 3 to Rails 3.

Even I’ve spent some time learning Ruby, I’m very happy with it, I love Ruby on Rails 3 over ASP.NET MVC 3. Much.

Ruby on Rails was built for high level of productivity in every aspect of it, that’s why I love RoR over ASP.NET MVC.

What I love on ASP.NET MVC is just it gives me control on everything of my web application.

I can hook and customize almost everything in the pipeline with ASP.NET MVC while I can focus on business value more in RoR.

Source code control, while I’m working on Visual Studio 2010, I decide to ignore Visual Source Safe as it will load my time, I’ve to setup the server and else…

While in open source world, I choose Git, and I’m not setting up it myself as I decide to work with GitHub.com and pay them to get some number of private repositories.

It is cool and by far easy to use.

For the coding IDE, nothing can beat the experiences got from Visual Studio. I tried Aptana Studio 3, RubyMine and no one can beat Visual Studio 2010!

While I’ve chosen JetBrains RubyMine as my primary IDE for RoR, I still need to use command-line to complete various things on RoR.

Another note, I really love Ruby Gems, the Bundler things… as I don’t have to find and pay for 3rd party components to integrate into my app, it almost FREE in RoR while in ASP.NET, it have been charged! High price!

Wow, after 3 months of self employed, I found I moved to open source platform surprisingly. I think I should consider to move more on open source world.

Here is my current development environment

My primary machine

  • PC with Windows 7 x64
  • Ruby
  • RubyGems + Rails 3
  • JetBrains RubyMine
  • Git

My secondary machine

  • Mac Mini with OS X Snow Leopard
  • Ruby
  • RubyGems + Rails 3
  • JetBrains RubyMine
  • Git

I’m planning to give my primary PC to my son and getting new iMac with Macbook Pro 17″ for further development.

Good bye to Windows?

- Absolutely no because some of my Internet marketing tools still rely on Windows OS. (Scrapebox – who can live without it?)

- At least, I should have a one copy of Windows on my virtual machine application.

Talk soon,

jQuery

Rails 3 DESTROY did not work! The solution…

Hi there,

If you are just beginning with Rails 3 and found some difficulty that made your rails app’s DESTROY command didn’t work for your scaffold views.

I think this is a quick troubleshooting guide with solution to get pass this stupid thing.

Problem #1 : You didn’t included the default Rails javascript files.

- Please make sure you have something like

javascript_include_tag :defaults

or at least

javascript_include_tag 'prototype', 'rails'

- Because the generated view for DESTROY command is based on those libraries and rails is using Prototype be default.

prototype

Problem #2 : Javascript conflicted between Prototype and jQuery.

- Because rails is using Prototype as default javascript library, so, if your view is using jQuery, when you’ve include both jQuery and Prototype, the DESTROY command will broke!

- Good thing is that Rails 3 is allow you to switch from Prototype to jQuery, the most easiest approach I found is to install gem ‘jquery-rails’ via your terminal or using Gemfile.

- After the gem was installed, run the command

rails g jquery:install

This will remove prototype and add jQuery into your project tree, if you are using Git, don’t forget to add those newly added files into your Git.

- Now just include the correct libraries, what I did is…


javascript_include_tag 'jquery.min.js',
'jquery_ujs', ...

The DESTROY command should work now without breaking your jQuery integrated views.

jQuery

Problem #3, missing of <%= csrf_meta_tag %>

Just in case if you are developing new app with new template, you might forget to include Cross Site Request Forgery tag, DESTROY command will not work, all sessions will be cleared without telling you what’s wrong.

This is because you are forgot to include the

<%= csrf_meta_tag %>

in your template.

Hope this help.

Heroku

Heroku Deployment Error => fatal: ‘heroku’ does not appear to be a git repository

While I’m exciting how much productivity was improved while using Ruby on Rails to code my web application, I found that it is quite hard to deploy the app, much harder than any traditional framework I found!

I almost success with Rails application deployment on shared hosting account at HostGator, but when the thing error, I can’t find where to see error logs.

No logs = No way to solve the problem easily.

So, I’ve did some research on Ruby on Rails deployment and found some cool deployment related platform, tools which I would like to list here.

  • Heroku – RoR deployment platform
  • EngineYard – RoR deployment platform
  • Capistrano – tool
  • RackspaceCloud – cloud server with ROI in mind in long run

From what I’ve learned and tried…

Nothing easier than Heroku, with just a few Git commands, you can have your app running in 5 minutes!

Heroku

These are commands you need to use with Heroku.

  • gem install heroku (install through RubyGems – sudo if needed)
  • heroku create your_app_name (create an application on Heroku)
  • git push heroku master (deploy to Heroku)

Your app will be ready at your_app_name.heroku.com.

Anyway, as I’m working in two places, my working room at day using Windows and bed room at night using Mac.

I control my source code through GitHub and first created Heroku app at my Mac.

Everything is fine, app can be deployed and running very quickly, I’m very exciting.

However, today I’ve modified my app at my Windows machine and try to deploy… I found…

  • I can push everything into GitHub properly.
  • I can not push/deploy to Heroku with the error => fatal: ‘heroku’ does not appear to be a git repositiry

My hair losses for a night after I’ve found a solution.

Let’s discuss about Heroku command.

heroku create your_app_name

This command will try to create an app at Heroku server and seeing if you already had ‘heroku’ repository or not, if ‘heroku’ repository doesn’t exists in your local Git. The command will create the ‘heroku’ repository of the active branch for you.

And this is the reason why I can’t deploy through my Windows machine as I didn’t run the heroku create command on this machine. I run it on my Mac.

So, if you are facing the same error as I got, please check if you already had the ‘heroku’ repo or not. By using this command.

git remote -v

I think you didn’t saw the ‘heroku’ repo there. So, to resolve the issue, please add ‘heroku’ repo manually using this command.

git remote add heroku git@heroku.com:your_app_name.git

Then, you will be able to do…

git push heroku master

This will help you solve the problem. :)

If you still got an error message, but not the same, might be “permissions denied (publickey)”, you might need to add keys to let Heroku know by this command.

heroku keys:add

Hope this help.

rails

Scaffolding in admin namespace with model in the root namespace

This is by far the most popular structure used by any web application.

To have ‘admin’ in sub-folder using to manage the administrative information.

Such as we have ‘category’ model, and we want to manage the scaffold in www.myrailsapp.com/admin/categories instead of www.myrailsapp.com/categories

Too bad, the generators provided by RoR didn’t included this.

Here are the steps you need to do this practices.

  • Generate a blank controller named ‘admin/categories’. (mentioned that it is ‘categories’, you have to manually ‘s’ it.)
  • Generate a scaffold ‘category’, the model ‘category’ will be generated while the ‘categories_controller’ and related views will be generated too.
  • Move all functions from ‘categories_controller’ to ‘admin/categories_controller’.
  • Delete ‘categories_controller’.
  • Move all views from ‘views/categories’ to ‘views/admin/categories’.
  • Delete the folder ‘views/categories’.
  • Edit the routes.rb and add this line :
    namespace :admin do resources :categories end
  • Edit the ‘admin/categories_controller’, modify the redirect_to to specify the namespace being used like this :
    redirect_to([:admin,@category], ...)
  • Edit the ‘admin/categories_controller’, modify the categories_url to admin_categories_url.
  • Now edit all views, do the same with link_to, if you found @categoy, just add :admin to specify the namespace.
  • Do the same in all views, change xxx_categories_path to xxx_admin_categories_path.
  • Then modify the form view, change from form_for(@category) to form_for([:admin,@category]).

It will works now.

Also, if the Destroy action didn’t work, didn’t popup a confirm message, please include the bundled ‘prototype.js’ and ‘rails.js’ into your template too.

Hope this help.

 

rails

Ruby on Rails Problem #2 : rake 0.9.0 (undefined command ‘task’)

Yup, this is the problem #2 of Ruby on Rails which I want to jot down into my blog.

It is an error message showing after I’ve executed ‘rails new …’ (yo, when attempting to create new rails application)

Problem : Error message is undefined command ‘task’.

Reason

This is somewhat a bug in rake 0.9.0 from what I’ve observe this problem over Google.

Solution

Simple by adding this piece of code…

module ::AppName
class Application
include Rake::DSL
end
end

module ::RakeFileUtils
extend Rake::FileUtilsExt
end

between the line…


require 'rake'

and


AppName::Application.load_tasks

Hope this help!

rails

Ruby on Rails Beginner’s Problem #1

Well, I just want to write a quick short post here about the problem I’m facing when beginning with Ruby on Rails on Windows while I didn’t found this problem when I’m riding Ruby on Rails on Mac OSX.

Problem #1 : error message when the database gems were installed but not required binaries. (sqlite3, mysql or else…)

Reason

This is just because you don’t have the required binaries for those database in the correct place.

Solution

Easy, just download the binaries of those database and put it into c:\Ruby192\bin folder or your ruby’s bin folder.

For sqlite3_ruby gem -> you need sqlite3.dll

For mysql gem -> you need libmysql.dll

Sqlite website allow you to download on compiled binaries, I’m not sure about mysql but I just copy the libmysql.dll from wamp/mysql/bin folder.

Hope this help.