Sunday, March 11, 2012

Rails 3.2 and Ruby 1.9.3 on Windows

RailsInstaller 2.1 for Ruby 1.9.3 and Rails 3.2 for Windows has been released! Go read about it at engineyard blog … and download it from railsinstaller.org !

It includes:


Wednesday, September 7, 2011

Rails installer 2.0 has been released

RailsInstaller 2.0.0 for Windows has been released! Go read about it at engineyard blog … and download it from railsinstaller.org !

Sunday, September 4, 2011

Duplicate a model in rails 3.1

Ever wanted to duplicate a model in rails ?

This blog post will answer the following issue common to almost all gui. In order to facilitate the life of the administrator, we often see the possibility to duplicate an existing record instead of creating a new one from scratch.

Before rails 3.1, this action was performed using the active-record clone method.

Rails 3.1 is introducing the dup method. Just by it's name, it seems to be the perfect candidate for our task.

How to use it ?

First let's test in the console:

rails c  

a = Model.first # finds the first instance of the model "Model"  
b = a.dup # duplicates the record  
b.save # saves the record into the database.

If you look into your database, you can see that a new record has been created. It is identical to the first record excepted that it has a new id.

You can also modify one of the field of the model to show to the user that he is working with a duplicate and that he has to edit it. I usually prefix the name with "dup_" so I get "dup_Georges" indicating that I need to modify the record.

b.name = "dup_" + b.name

That's it. A simple but very useful method. In my opinion it's the 8th method that has been forgotten in the crud scaffold.

Some details about the implementation:

# ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.

# Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
# Calling ActiveRecord::Base#dup will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.

Tuesday, April 19, 2011

Monday, September 13, 2010

access SQLServer 2008 with Rails 3 from Ubuntu Lucid Lynx 10.04

This tutorial is meant to be used with rails 3 final version, ruby 1.9.2 final version too.

The purpose is not to install rails 3 or ruby 1.9.2, tutorials to do so are mentioned in my 2 earlier posts of August and September 2010. The idea here is to connect from an Linux Ubuntu 10.04 station (or server) where you have your rails application to a Windows station (or server) where your SQL Server 2008 database resides.

First, to connect your machine, you need to be able to perform a connection to your database. To do so, you'll have to install FREETDS and UNIX-ODBC.

1. Install FREETDS

sudo apt-get install freetds

2. Configure FREETDS

edit the file /usr/local/etc/freetds.conf

insert the following text:

[TDS]
host = NAMEOFWINDOWSHOST (or an IP address like 192.168.0.130 if you are not on a domain)
port = 1433
tds version = 8.0

3. Test FREETDS

tsql -S 192.168.0.130 -p 1433 -U username -P password

You should see a prompt if it works. Then you can go ahead and parameterize UNIX-ODBC

4. Install UNIX-ODBC

sudo apt-get install unix-odbc

5. Configure your ODBC

edit the file /etc/odbc.ini

insert the following text:

[192.168.0.130]
Driver = FreeTDS
Description = ODBC Connection via FreeTDS
Trace = No
Servername = 192.168.0.130
Database = MySQLDatabaseName


edit the file /etc/odbcinst.ini

insert the following text:

[FreeTDS]
Description = TDS Driver (Sybase/MS SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout = 
CPReuse = 
UsageCount = 1


6. Test UNIX-ODBC

isql 192.168.0.130 username password

The ip address here can be replaced by the name you have on top of /etc/odbc.ini (it's the name of your ODBC). (if you have a desktop version, you can also install ODBCConfig to edit the ODBC connection).

if it went correctly, you should be able to query your database with SQL (Warning, the output will look a bit messy but it's just a connection test after all).

The second part is to verify that the firewall on your network or machine allows a connection to your SQL Server. Usually you have to specify a rule in your firewall to allow the Unix machine to 'talk' to the Windows machine using the port 1433 (or any other port if you changed the default port for TCP communications in your SQL Server configuration).

The third part is entering (at last) the ruby and rails world. You will need to install RUBY-ODBC (at least version 0.99992) and the ACTIVERECORD-SQLSERVER-ADAPTER (at least version 3.0.0)

The current version that comes when you do GEM INSTALL 'RUBY-ODBC' does not work with Ruby 1.9.2 because the STR2CSTR has been deprecated in Ruby 1.9.2. So you'll have to install this manually (details can be found in this thread)

download the fixed version at http://www.ch-werner.de/rubyodbc/ruby-odbc-0.99992pre3.tar.gz

uncompress it in 'ruby-odbc-0.99992pre3', go to the directory and install it using the following commands:

ruby -Cext extconf.rb
make -C ext
make -C ext install


Note: more details are available in the README file in the same directory.

Then install the activerecord-SQLServer-adapter

gem install activerecord-sqlserver-adapter

(or add it to your Gemfile).

Then edit your application and modify the config/database,yml file as follows:

development:
  adapter: sqlserver
  mode: odbc
  dsn: 192.168.xxx.xxx # This is your DSN name (here the IP is the DSN name too).
  username: testdummyusername
  password: testdummmypassword
  host: ILOVEMYWINDOWSPCNAME #not necessary

That's it. Now you can test your application to see if you can connect.

RAILS SERVER

Sunday, August 22, 2010

Ruby 1.9.2 for windows is released

Ruby 1.9.2 for windows is released, You can find the binaries here. You will also find all instructions required for installation.

An example of setup with rails 3 was documented on this blog.

Saturday, August 21, 2010

RVM issues while setting up Ruby 1.9.2 and Rails 3 rc2 on Lucid Lynx

In order to test rails 3 and ruby 1.9.2, I've migrated to Ubuntu 10.04. During the installation I ran into a few issues and had conflicts with both my version of OS and my versions of ruby.

Here is the list of the lessons learned :

Rule number 1 : always install the latest version of RVM (it's updated very frequently). gem install rvm

Rule number 2 : Go and modify your bash files when it tells you to do so (I did not do it at first and it completely messed up my later installs, with RVM mixing old and new paths for gems).

Rule number 3 : On lucid lynx (Ubuntu 10.04), you cannot update the rubygems by doing gem update -system. It's likely that you will have version 1.3.5 as it is the official version supported by the distribution. To upgrade to the required 1.3.7, follow this procedure :

sudo gem install rubygems-update
sudo rubygems_update


when you do gem --version
you should now see > 1.3.7 instead of > 1.3.5

if you still see 1.3.5, i suggest you to do the following before the above procedure:

sudo apt-get remove rubygems

Rule number 4: install the pre-requisites for Rails 3 rc2

gem install tzinfo builder memcache-client rack rack-test erubis mail text-format bundler thor i18n


Rule number 5: install rails

gem install rails --pre

VoilĂ , you can now start to play or upgrade your apps (see the screencasts below).

As soon as Luis Lavena releases the new 1.9.2 ruby installer I will try and document the same process on windows (with PIK, the windows equivalent of RVM).

There are a lot of great Rails 3 tutorials available now. If you start I recommend the railscasts numerous episodes on the topic or the envylabs screencasts made for the latest railsconf. They both do a fantastic job for the community.

For the help with the install, the credits go to the people in this thread.

Thursday, August 19, 2010

Thursday, August 12, 2010

How to develop live search textbox in Ruby on Rails

A nice tutorial for a live search textbox using jquery can be found here

Saturday, June 12, 2010

Using jquery flexselect (a predictive select control) in your rails views

This tutorial will show how to use Jquery flexselect with rails. It is the first part of a series of tutorials on rails UI showing how to enhance the basic rails view in an unobstrusive way.

Flexselect replaces a select control with a textbox control where you can type anything. While typing, flexselect will instantly show you a list of corresponding matches within the select list. The main advantage of this control is to allow the instant search in a long list of options (replacing the painfull user-experience which leads to display annoyances with a normal select control). You can type characters in any order and flexselect will still show you a match. If the user has javascript disabled, the normal select control will render and your page will still work.

For the tutorial, a small application will be build from scratch :

1. In the console, type

rails flexselectdemo 

You will notice the usage of the default sqlite3 database (you can change database.yml in /config to use any other database adapter).

2. create a basic scaffold for a post

ruby script/generate scaffold post name:string author:string 

3. Edit the view to add the jquery framework and flexselect stylesheets in the head section

<%= stylesheet_link_tag 'scaffold' %>
  <%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css", :media => "screen", :cache =>  true  %>
  <%= stylesheet_link_tag 'flexselect', :media => "screen", :cache =>  true %>


4. Edit the view to add the jquery framework and flexselect javascripts at the end of your body section

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",:cache => true %>
    <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js",:cache => true %>
    <%= javascript_include_tag  "application", :cache => true %>
    <%= javascript_include_tag  "flexselect/liquidmetal", :cache => true %>
    <%= javascript_include_tag  "flexselect/jquery.flexselect", :cache => true %>

5. Copy the files from the flexselect plugin to the relevant public directories:

the jquery.flexselect.js and liquidmetal.js files go to your public/javascripts/flexselect/ directory

the flexselect.css file goes to your public/stylesheets directory

6. Edit the post edit view:

Replace

<%= f.label :author %>

    <%= f.text_field :author %>
  

with


<select class="flexselect">
                 <option value="1">George Washington</option>
                 <option value="2">John Adams</option>
                  <option value="3">Thomas Jefferson</option>
                  <option value="4">Bill Gates</option>
                  <option value="5">Nicolas Sarkozy</option>
                  <option value="6">Gordon Brown</option>
                  <option value="7">Angela Merkel</option>
                  <option value="8">Moritz Leuenberger</option>
                  <option value="9">Barrack Obama</option>
            </select>

This will give you



for the select box and 


for the flexselect box

7. Add the following to application.js in the public/javascripts folder

// Jquery : flexselect plugin
jQuery(document).ready(function() {
  $("select[class*=flexselect]").flexselect();
});


8. Create the database and execute the migration

rake db:create:all 
rake db:migrate 

9. Launch the server

ruby script/server 

When you edit a post, you should see a textbox instead of the select box. If you type inside, you will see immediately the available options in a list below.

This ends up this tutorial on the implementation of jquery flexselect with ruby on rails. Note that the tutorial was simplified compared to a real world implementation (ex. jquery is loaded from google repository and direclty in the view / flexselect is not yielded by the chosen model but is in the general view and so on).

You can find flexselect here.

Wednesday, June 9, 2010

Datamapper SQL Server adapter

In case you're looking for it, here is the Gem for the SQLServer datamapper adapter and here is the release note of the latest release (v1.0) of datamapper.

DataMapper In 20 min

Check out this SlideShare Presentation:

Replacing ActiveRecord With DataMapper

Check out this SlideShare Presentation:

Legacy Database with Datamapper

Oh yes, we do fight sometimes with our sqlserver legacy database. So anything that can ease the pain is welcomed.

Sunday, May 16, 2010

Why MongoDB is awesome

And we continue on the MongoDB serie with a presentation by the creator of MongoMapper for Ruby.

Saturday, May 15, 2010

MongoMapper - Mapping Ruby to and from Mongo

If you want an alternative database solution, more document oriented, check out this SlideShare Presentation:

Monday, May 3, 2010