Problem
After upgrading to Rails 2.0.2 when trying to install the sqlite3-ruby gem got the following error:

ERROR:  Error installing sqlite3-ruby:
ERROR: Failed to build gem native extension.
/usr/bin/ruby extconf.rb install sqlite3-ruby
checking for sqlite3.h... no
make
make: *** No rule to make target `ruby.h', needed by `sqlite3_api_wrap.o'.  Stop.
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1/ext/sqlite3_api/gem_make.out

Solution
In Mandriva you need to install the ruby-sqlite3 and the development libraries first, like:

sudo urpmi ruby-sqlite3
sudo urpmi libsqlite3-devel

After that you should be able to install the gem as normal.

Problem
Upgrading from Rails version 1.2.3 to version 2.0.2.
Following the suggestion in DH’s weblog here when using sudo gem install rails -y, it gets the trunk version of rails which at the time was 2.0.2.9216.
This causes a few problems and breaks a few more things.

Solution
In order to get back to the latest stable version, you have to uninstall a few of the gems installed from the trunk version.
If you have installed the latest version of top of an existing one using gems, then doing:

gem list --local

will give you all the installed versions of the gems. Something like:

actionmailer (2.0.2.9216, 2.0.2, 1.3.3)
actionpack(2.0.2.9216, 2.0.2, 1.3.3)
rails(2.0.2.9216, 2.0.2, 1.2.3)
...

Make sure you uninstall all the gems with version number 2.0.2.9216:

sudo gem uninstall rails
sudo gem uninstall actionmailer
sudo gem uninstall actionpack
sudo gem uninstall activerecord
sudo gem uninstall activeresource
sudo gem uninstall activesupport
sudo gem uninstall rails

In each of the above commands the gem package manager should ask you to select the one you want to uninstall.
Pick the one with the 2.0.2.9216 (or other version later than 2.0.2).

At the end if you list the gems again you should only have gems going up to version 2.0.2

An extra step I had to take for making the migrations work was to install rake again:

sudo gem install rake

After all these my installation seems to be working fine again.

Problem
You want to have validations for phone numbers in Ruby on Rails code.

Solution
The mobile numbers in UK should start with a 0, followed by 7, then a number between 5 and 9, and finally another 8 numbers.
For the landline numbers in UK the number should start with 0, followed by either 1 and 8 or 9 digits, or by 2 or 3 and then 9 digits.
So in order to use a regular expression in your validations, you could use something like the following in your model:

case phone_no_type

  when 'mobile'

    if phone_no !~ /\A(([0][7][5-9])(\d{8}))\Z/

      errors.add(’Mobile number”)

    end

  when ‘landline’

    if phone_no !~ /\A([0])((([1])(\d{8,9}))|(([2-3])(\d{9})))\Z/

      errors.add(’Landline number’)

    end

  end

Problem
You need to display a splash screen when uploading files to a Ruby on Rails application.

Solution
My model:

class Number < ActiveRecord::Base
  file_column :intro
  file_column :vmail
end

The steps I have followed.

  1. Install redbox plugin from project path run:
    ./script/plugin install svn://rubyforge.org/var/svn/ambroseplugins/redbox
  2. If files redbox.js, redbox.css and redbox_spinner.gif haven’t been copied over to the public folders copy them over manually or go to vendor/plugins/redbox and run:
    rake update_scripts
  3. Find an animated gif for the progress indicator to be used (ie pleasewait.gif) and copy it over to public/images.
  4. In order to be able to use the progress indicator in different parts of the application, add the following to the app/views/layouts/ :
    application.rhtml
                 <%= stylesheet_link_tag 'redbox' %>
                <%= javascript_include_tag 'redbox' %>

    near the place where <= javascript_include_tag :default > is located.
    Also at the bottom of the page create the content of the redbox:

    <p id="redbox" style="display: none"> </p>
    
    <p style="color: blue"> Please wait ....
    
    Uploading files
    
    <img src="/images/pleasewait.gif" width="200" />
    
  5. Copy _update_form.rhtml, update_form.rhtml and update.rjs from vendor/plugins/active_scaffold_upload/frontends/default/views/ to the app/views/numbers (model you want to use).
  6. To display the redbox in the numbers/_update_form.rhtml add the following in the submit_tag:
     , :onClick => "return RedBox.showInline('redbox') " %>
  7. To close the redbox after the uploading was successful, add the following in numbers/update.rjs as the first line inside controller.send :successful? clause:
    page.RedBox.close()

Problem
You want to use cvs to checkout a repository from a remote server.

Solution
If you were using subversion it would be very simple to do by using the remote url.
In CVS you have to to do the following:
First find out where the cvsroot in the remote server is. We will assume for this example that is located in /usr/local/cvsroot.
Then run the following on your local linux pc:

export CVSROOT=:ext:user_name@remote_pc:/path_to_csvroot export
export CVS_RSH=ssh

You then should be able to do a checkout by typing the following and supplying your password:

cvs checkout project_name

Problem
Installing Ruby on Rails on CentOS 4.6.
The yum install ruby installs an old version of Ruby 1.8.1, but you want 1.8.6
Solution
Based on a script from uberdose

The original post is here:
http://wp.uberdose.com/2007/07/15/ruby-on-rails-on-centos-45/

  wget ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz
  tar xvfz readline-5.1.tar.gz
  cd readline-5.1
  ./configure --prefix=/usr/local
  make
  sudo make install

  cd ..
  wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.gz
  tar xvfz ruby-1.8.6.tar.gz
  cd ruby-1.8.6
  ./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local --enable-shared
  make
  sudo make install

  cd ..
  wget http://rubyforge.org/frs/download.php/20980/rubygems-0.9.4.tgz
  tar xvfz rubygems-0.9.4
  cd rubygems-0.9.4
  sudo /usr/local/bin/ruby setup.rb
  cd ..
  sudo gem install rails --include-dependencies
  sudo gem install termios --include-dependencies
  sudo gem install mongrel --include-dependencies
  sudo gem install mongrel-cluster --include-dependencies
  sudo gem install capistrano --include-dependencies
  sudo gem install mysql -- --with-mysql-dir=/usr/

Problem
You have just upgraded your Capistrano installation to version 2.x, but you didn’t have time to convert your recipes to the newer version.

Solution
In order to be able to use your old recipes of Capistrano version 1.x after upgrading, you can use the following (assuming your version 1.x is 1.4.1):

cap _1.4.1_ deploy

or, if you have migrations you want to use:

cap _1.4.1_ deploy_with_migrations

Problem
You want to be able to login to different servers with ssh, but don’t want to be using your password every time.

Solution
We assume that you already have installed ssh and have created ssh public keys in your local machine.

  1. Run the following in your local machine:
    ssh-copy-id -i ~/.ssh/id_rsa.pub user_name@remote_host
  2. If you want to add aliases in your bash profile so you don’t have to type the whole address of the remote host, edit your .bashrc and add:
    alias short_name="ssh user_name@remote_host"
  3. Restart your X server
  4. You should be able to login with ssh without using your password by typing in your command prompt in your local machine:
    short_name

Problem
You have a lot of ps files and you want to combine them into a single pdf file with multiple pages.

Solution
You can use Ghostscript:

gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.ps in2.ps in3.ps

Problem
You want to use the easiest authentication method, in order to add users/permissions to your application.

Solution

  1. Install the act_as_authenticated plugin:
    script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
  2. Generate controllers, models and migration:
    script/generate authenticated user account
  3. Add necessary user foreign keys in appropriate tables (ie customer in xxx_create_users.rb), plus other fields you may want to use in your user table (ie role):
    create table "users", :force => true do |t|
      t.column :login, :string
      ...
      t.column :role, :string, :default => 'C'
      ...
      end
      add_column :customers, :user_id, :integer
      ...
      def self.down
        ...
        remove_column :customers, :user_id
      end
  4. Run the migration:
    rake db:migrate VERSION=xxx
  5. Comment out from app/controllers/account_controller.rb the following:
    include AuthenticatedSystem
  6. Add it in the app/controllers/application.rb just under the class declaration.
  7. Add in application.rb (same as above) the following just after the session section:
    before_filter :login_required, :except => [:login, :signup, :logout]
  8. Comment out the default redirection after login and put your own (ie customers): in the account_controller.rb, just before
    the flash[:notice] = “Logged in successfully”.
    Also add the else for the the invalid login:

      redirect_back_or_default(:controller => 'customers', :action => 'list')
      flash[:notice] = “Logged in successfully”
    else
      flash[:notice] = “Invalid Login/Password!”
  9. Change the action in the signup and logout functions in the account_controller.rb file from index to login
  10. Add (optionally) more fields (role) in the signup page (app/views/account/signup.rhtml):
    
    
    
    <%= f.select :role, ['A','R','C'] %>
  11. Add restrictions for displaying records depending on user logged in, by using the conditions_for_collection of the activescaffold plugin in app/controllers/customers_controller.rb:
    def conditions_for_collection
      ['customers.user_id = (?)', current_user.id]
    end
  12. Add a menu page layout in app/views/layouts/_menu.rhtml:
  13. Add the call to the menu partial in the app/views/layouts/application.rhtml, just before the div with id=main :
    <%= render :partial => “layouts/menu”%>

« Previous PageNext Page »