Howto install different version of nginx with Phusion Passenger (standalone version)?

Phusion Passenger aka mod_rails + Apache2 or + Nginx is the easiest way to deploy Rails 3 app on the market. Thanks to the great team at Phusion, you could install standalone version and test out your app like you run test out with WEBrick and it’s just easy as 1,2,3.

Phusion Passenger standalone version install with nginx 0.8.5.4 by default. Now what if you’re running different nginx version on your production and you want to make sure you match that version in your development? Here’s how

passenger start --nginx-version VERSION

where VERSION is the desired version. For example, if you want to try out the shiny and hot nginx 1.0, simply type in your rails3 folder in terminal

passenger start --nginx-version 1.0.0

Enjoy!

Compile Ruby 1.9.2 and Ruby 1.9.3 with Clang (Xcode 4) under RVM on Mac OSX 10.6.7 or 10.7.0

In my previous post, I tried to compile Ruby 1.9 but didn’t have much success, although the compilation completes, the RoR doesn’t function correctly (I got Segmentation fault while running ‘rails s’). Today post will be about my second attempt to get Ruby 1.9 compiled with more recent version of clang, ver 2.1 (included in XCode 4.1 by Apple – free to download from AppStore if you were on OSX Lion).

clang 2.1 is based on LLVM 3.0SVN. I’ve done few researches on this and been reported people get 1.9.1-p0 successfully compiled but none has success with 1.9.2-p290. I am very curious if clang 2.1 would get all nasty exceptions resolved.

Prerequisites
Firstly, you need to download Xcode 4.1.1 from Apple website. Then set up Environment like this:

export rvm_clang_flag=1
export CC=/Developer/usr/bin/clang
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
export PATH=/Developer/usr/bin:$PATH

Secondly, you need RVM installed and ruby packages fetched. For this experiment, I fetch both stable 1.9.2-p290 and 1.9.3-p0 version:

rvm fetch 1.9.2
rvm fetch 1.9.3

Now let’s get started, issue configure and install command for RVM:

ruby 1.9.2-p290

rvm install 1.9.2 --reconfigure --debug -C --enable-pthread --with-gcc=clang

After running for few minutes, I got a whole full screen with garbage code and junk, checking log file indicates that clang failed to get 1.9.2 compiled. The log can be found here http://pastebin.com/bwZ4UYiY

That doesn’t look good, at least I got 1.9.2rc1 successfully compiled with clang 1.5 before. So there must be changes in p290 that breaks.

ruby 1.9.3-p0
Come to think of it, there might be high chance ruby 1.9.3 has resolved all those mysterious issues so why not give it a try?

First, you need to install libksba first, I no longer use portage, so in this article I use Homebrew

brew install libksba

Now we install Ruby

rvm install 1.9.3-p0 --reconfigure --debug -C --enable-pthread --with-gcc=clang
rvm reload
rvm use 1.9.3-p0

Yes! Ruby compilation reaches the end of the process though there is a minor issue. The process seems break rvm and a quick fix is to close the Terminal and re-open.

Now let’s give RoR a try with 1.9.3. And bravo, it works. Soon Apple will switch to use clang 3.0 by default in future release of Xcode, seeing Ruby 1.9.3-p0 compiled indicates a positive sign that OSX always be a favourite platform for Ruby/RoR development.
RubyOnRails 3.0.4, Ruby 1.9.3-head (11-02-2011) compiled with clang 2.0 (XCode 4.0 GM)

Howto install Rails 3.0, Ruby 1.9.2, MySQL 5.1 on Ubuntu Lucid 10.04 LTS or Ubuntu Maverick 10.10 or Ubuntu Natty 11.04 or Ubuntu Oneiric 11.10

Prerequisites

For this tutorial, I will show you how to install Ruby 1.9.2 and Rails 3.0 from source because Ubuntu repository only has the outdated Ruby 1.9.1 package. To compile Ruby from source, you need to install compilers and libraries headers.

NOTE: This tutorial can be applied for Ubuntu Maverick 10.10. The only difference is installing libreadline6-dev instead of libreadline5-dev. But I highly recommend you guys to stay with Lucid 10.04 LTS for production server.

$ sudo apt-get -y install libc6-dev libssl-dev libmysql++-dev libsqlite3-dev make build-essential libssl-dev libreadline5-dev zlib1g-dev

Install Ruby 1.9.2 from source

$ cd /tmp
$ mkdir src
$ cd src
$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.zip
$ tar xjvf ruby-1.9.2-p290.tar.bz2
$ cd ruby-1.9.2-p290
$ ./configure --prefix=/usr/local
$ sudo make && sudo make install

You can check if Ruby has been installed successfully by

$ ruby -v

and it should output

ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

Should you see the “Happy new Ruby” returned, then your installation is ready to go

ruby -ropenssl -rzlib -rreadline -e "puts 'Happy new Ruby'"

Ruby 1.9 bundles RubyGem so you don’t have to install it

Install SQLite3 and MySQL 5.1

$ sudo apt-get install mysql-server-5.1 sqlite3

Please make sure you set a password for MySQL server.

Install Rails and essential gems

$ sudo gem update --system
$ sudo gem install mysql2 rails rack rake mysql sqlite3
$ sudo gem install ruby-debug19 -- --with-ruby-include=./ruby-1.9.2-p290/

We can check if all gems are installed successfully

$ gem list

and it should list

abstract (1.0.0)
actionmailer (3.0.9)
actionpack (3.0.9)
activemodel (3.0.9)
activerecord (3.0.9)
activeresource (3.0.6)
activesupport (3.0.9)
...

Now you can generate your own Rails app.

$ rails new demo -d mysql
$ cd demo
$ rails server

You should be able to see a welcome screen under your web-browser (http://0.0.0.0:3000/).

That’s all for now. See you in next tutorial.

Get public path with Ruby On Rails – Tip #1

Tip #1: How do I get the path to ‘public’ folder?

Answer:

Normally people do:

path = Rails.root.to_s + "/public"

But I do this in a better way:

path = Rails.public_path

Works with RoR 2.3.x and 3.x

Howto make Paperclip to re-process images w/o deleting images

If you add new styles into your Image model and want Paperclip to process those styles w/o deleting existing images, you simply goes into your RoR console and type:

Image.all.each &:reprocess!

Howto enforce SSL on all front-end pages in Spree 0.11.x

In Spree production mode, SSL mode is activated if running with /admin (backend) but it’s not activated on front-end pages (non /admin). So how to enforce SSL on every frontend pages? Easy, simply extend Spree:BaseController like this in your SiteExtension:

class SiteExtension < Spree::Extension
  def activate
    Spree::BaseController.class_eval do
      protected
      def ssl_required?
        ssl_supported?
      end
    end
  end
end

Get stuck with OptionType/OptionValue complex with Spree – HELP!

Above is a very simple Spree product with many variants. I just have very simple question:

“If I know the OptionValue of Red, how could I retrieve all variants of the product that has this OptionValue?”

I looked around and found function has_option but this function does not limit the variants to a specified product… I am kinda new to Spree and stuck here.. can someone help?

OPTIMAL WAY Thanks to Roman Smirnov for the AR join way, this way is better than Steph’s in performance

product.variants.joins(:option_values).where(["option_values.name = ?", "Red"])
Variant.joins(:option_values).where(["option_values.name = ?", "Red"])
Product.joins(:variants =>  ption_values).where(["option_values.name = ?", "Red"]).group_by_products_id

UPDATE: Thanks to Stephanie of Endpoint, it’s just few basic AR query:

product.variants.select { |v| v.option_values.include? OptionValue.find_by_name(‘Red’) }

or, to get all the variants with that option value

Variant.all.select { |v| v.option_values.include? OptionValue.find_by_name(‘Red’) }

or, to get all products with variants with that option value

Variant.all.select { |v| v.option_values.include? OptionValue.find_by_name(‘Red’) }.collect { |v| v.product }.uniq

The dilemma of Rails.root vs RAILS_ROOT complex

Since Rails 2.3.6, Rails Core Team’s recommended users to take advantage of Rails.root in preference to the old RAILS_ROOT instance variable. The good thing about Rails.root is that it returns a Pathname object so you could do something like this:

File.join(RAILS_ROOT, 'public')

to

Rails.root.join('public')

It definitely looks neater right? But the core team did not mention what convention for other cases such as

path = "#{RAILS_ROOT}/public"

should we convert it to:

path = Rails.root.join('public')

OR

path = "#{Rails.root}/public"

OR

path = Rails.root.to_s + "/public"

It is up to your own taste. IMHO, I prefer to use the "#{Rails.root}/public" to others. What do you think?

Updated: For path to public folder, the best way is to use

Rails.public_path

Hope this helps :)

Howto verify the route of an URI in Rails?

So if you have an URI like this ‘/history/3′ and you want a quick way to figure out which controller or action that handle this routing, the normal and bad way is to go through the ‘config/routes.rb’ and work out the route. There is a better way:

$ ./script/console
Loading development environment.
>> irb ActionController::Routing::Routes
>> >> recognize_path(“/history/3”)
=> {:controller=>”auctions”, :action=>”history”, :id=>”3”}

You can past paramater :method into the recognize_path to verify route for method POST, GET, PUT and DELETE. For example, for POST:

recognize_path(“/history/3”, :method => :post)

Howto dump Rails routes?

The easiest way to dump all the routes out to the screen is with the Rake task ‘routes’:

$ rake routes

OR if you can dump it within the Rails console

$ ruby script/console
ruby-1.8.7-p249 > rs = ActionController::Routing::Routes
ruby-1.8.7-p249 > puts rs.routes

rs is object of class ActionController::Routing::RouteSet which consist of an Array of ActionController::Routing::Route objects.

Then how to test the output of a named route?

$ ruby script/console
ruby-1.8.7-p249 > # app.<named routes>
ruby-1.8.7-p249 > # assume you have the posts_path
ruby-1.8.7-p249 > app.posts_path
=> "/posts"

Hope you find this tip handy :)

Follow

Get every new post delivered to your Inbox.

Join 60 other followers