About ruby on rails, hackintosh, iphone3g and etc.

Tuesday, December 15, 2009

Install Nokogiri on Leopard

If you have this problem:

HI. You're using libxml2 version 2.6.16 which is over 4 years old and has
plenty of bugs. We suggest that for maximum HTML/XML parsing pleasure, you
upgrade your version of libxml2 and re-install nokogiri. If you like using
libxml2 version 2.6.16, but don't like this warning, please define the constant
I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before requring nokogiri.

Here is the solution:

Install the latest libxml2 and libxslt

Make a source directory somewhere and do the following commands to grab the latest versions of libxml2 and libxslt:

$ curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.3.tar.gz

$ curl -O ftp://xmlsoft.org/libxml2/libxslt-1.1.24.tar.gz

Once done, unpack both like so:

$ tar xzf libxml2-2.7.3.tar.gz

$ tar xzf libxslt-1.1.24.tar.gz

Now change into the libxml directory and build it:

$ ./configure

$ make

$ sudo make install

Now jump back to the libxslt directory and do the same:

$ cd ../libxslt-1.1.24

$ ./configure

$ make

$ sudo make install

If you want, go ahead and delete the sources to save space. Now you’re ready for the final step.

Install Nokogiri

Now installing Nokogiri is super easy once we tell it where to link.

$ sudo gem install nokogiri -- --with-xml2-include=/usr/local/include/libxml2 --with-xml2-lib=/usr/local/lib


Saturday, December 5, 2009

CORE_RL_magick_.dll not found

For Windows Environment Only

This error happens when during installation of ImageMagick, the path is not set, you can set the path manually at my computer, environment.

set PATH: C:\Program Files\ImageMagick-6.5.6-Q8

Upon this, if you still encounter error like R6034, it means the dll has some problem, need to install MS VC++ 2008 Redistributable.


Using RMagick to add text to image

Here is the way to do it:

require 'rubygems'
require 'RMagick'
include
Magick

image
= Image.new(50, 50) {
self.background_color = "white"
}
text
= Draw.new
text
.annotate(image, 0,0,0,40, 'Named Colors') {
self.fill = 'black'
self.pointsize = 32
}
image
.write("image.png")

If you encounter error in mongrel like

Mongrel not starting: `initialize_without_backlog': Cannot assign requested address, it may be because if you use rmagick and the process keeps running as it cannot find the requested font. Then you need to find the process like ruby script/server. look for the process id.

use this command in OSX: ps -ef

look for the line ruby script/server
get the pid (e.g. 925652)
then use this command

kill -9 925652

Installing RMagick in Snow Leopard

This entry is extracted from this blog:http://blog.d27n.com/2009/08/26/mac-os-x-snow-leopard-rails-mysql-and-sphinx/

Today I also got my SnowLeopard copy and updated my 10.5 System to 10.6. Yeah well, as you, I had some problems with the RoR installation but after about 4 hours I finally got it.

In your blog, you asked for some tips about the RMagick installation. First of all, the mentioned script installs some outdated versions, that’s why I used another way. First of all, I installed the current version of MacPorts (for SnowLeopard).

http://distfiles.macports.org/MacPorts/MacPorts-1.8.0-10.6-SnowLeopard.dmg

Then I tried just to install ImageMagick, that failed for me, because of libxml2 compiled for the i386 architecture. So you have to recompile / update it first.

sudo port install libxml2
sudo port install ImageMagick

It was enough for to have a running x64 version of ImageMagick on SL. It could be, that some other libs are compiled in i386, too. Than you have to update it first before compiling ImageMagick.

My next step was the installation of the RMagick gem. It’s maybe a bug, but after my first run of

sudo gem install rmagick

I get the error, that rmagick was configured for a wrong version if ImageMagick, after the second run of the gem install rmagick, I hat a working solution.

Thursday, December 3, 2009

Enable Multiple Buttons in a Form in Rails

Sometimes we need more than one button in the form. To do so in Ruby On Rails, we have to do the following:

Create a method in the controller to accept the form input
e.g

def approve_form
case params[:submit]
when "Approve"
redirect_to :action=>"approve"
return
when "Reject"
redirect_to :action=>"reject"
return
end
end


in your form, do this

Wednesday, December 2, 2009

link_to popup error in IE

If you encounter problems that the popup does not appear in link_to function in IE browser, below will solve your problem.

Make sure there is no space between the names of the window

e.g.

link_to "View This Image", :action=>:show_image, :popup=>['window_name', 'height=300,width=300']

window_name should not have spaces.

Thursday, November 19, 2009

Rails Model Generate and Destroy

Sometimes for beginner's to Rails, generating model and not knowing how to delete or remove the unwanted model.

To generate a model called cart:

script/generate model cart

To remove this model called cart:

script/destroy model cart


Tuesday, November 17, 2009

Cannot install rails. Set that proxy.

If you are installing gems or rails over a proxy, you may encounter the following problems.

Remember to set your proxy first as below:

In Windows:
gem install rails
ERROR: http://gems.rubyforge.org/ does not appear to be a repository

Before we begin, if you are behind a proxy, open the command prompt and give the following command:

 >Set HTTP_PROXY=http://:

For example, if the address of the proxy server is 192.168.1.1 and the port number is 9090, then you would have to give the following at the command prompt:

 >set HTTP_PROXY=http://192.168.1.1:9090 

Next, give the following command at the prompt:

 >gem update

Monday, October 19, 2009

Cannot Remove WD SmartWare. Disable It.


Here’s another way to keep the virtual CD volume from mounting (tested on Leopard):

1. Connect the new WD My Passport or other drive “infected” with this firmware
2. Open a Terminal and type something like:

diskutil info “/Volumes/WD SmartWare”

(this corresponds to the volume you want to get rid of)

3. Copy the large string of numbers that corresponds to “Volume UUID:” in the info.
4. Edit /etc/fstab as root using vifs:

EDITOR=nano sudo vifs

(if you want your usual editor, drop the “EDITOR=nano” part)

5. Add a line like the following, putting your UUID in from step 3:

UUID=that_big_number_from_step_3 none hfs ro,noauto 0 0

6. Save the file to quit out of vifs
7. Disconnect the My Passport and attempt to plug it back in and watch!

It’s a bit more involved than DSW, but you only have to do it once and the virtual CD volume won’t remount. WD really need to issue a firmware update that removes this virtual drive. At least give some of us the option of not having it!

Thursday, October 15, 2009

Text mode in browser

Found an interesting service called viewtextmode.com. It is a bookmarklet based service which will strip out all images, embedded contents and others leaving only text on the page. Clean and clear. This makes easier to read blog post as well as follow up on news.

Here is the url:

Need to register to start to use the service.

Wednesday, October 7, 2009

The terminal server has exceeded maximum number of allowed connection, mstsc


  • Open Command Prompt.
  • Type: "mstsc /admin".
  • Remote Desktop Connection will start. Type the computer name or IP address of the computer you want to connect to in the Computer box.
  • Configure any other desired options, and then click Connect.
  • Thursday, October 1, 2009

    MySQL & Rails in Windows

    MySQL installation in Windows for Ruby on Rails usage can be problematic. With version 5.0, this seems to work. Upon installation, remember to execute:

    gem install mysql

    Following that, if you encounter error during rake db:migrate with the error indicating

    Mysql::Error: query: not connected: SELECT version FROM schema_migrations.

    you can try to solve it by copying the version 5.0 libmySQL.dll to your Ruby\bin folder.

    libmySQL.dll is located in mysql\MySQL Server 5.0\bin directory.


    Sunday, September 20, 2009

    uninitialized constant ApplicationController

    Upgrading from Rails 2.2.2 to later version namely Rails 2.3 and above, there is simple change which stop your Rails app from running. To update all the incompatibilities, namely changes due to application.rb to application_controller.rb, you can use this command:

    rake rails:update

    That's all.

    Thursday, August 13, 2009

    Ruby Date Time

    %a - The abbreviated weekday name (``Sun'')
    %A - The full weekday name (``Sunday'')
    %b - The abbreviated month name (``Jan'')
    %B - The full month name (``January'')
    %c - The preferred local date and time representation
    %d - Day of the month (01..31)
    %H - Hour of the day, 24-hour clock (00..23)
    %I - Hour of the day, 12-hour clock (01..12)
    %j - Day of the year (001..366)
    %m - Month of the year (01..12)
    %M - Minute of the hour (00..59)
    %p - Meridian indicator (``AM'' or ``PM'')
    %S - Second of the minute (00..60)
    %U - Week number of the current year,
    starting with the first Sunday as the first
    day of the first week (00..53)
    %W - Week number of the current year,
    starting with the first Monday as the first
    day of the first week (00..53)
    %w - Day of the week (Sunday is 0, 0..6)
    %x - Preferred representation for the date alone, no time
    %X - Preferred representation for the time alone, no date
    %y - Year without a century (00..99)
    %Y - Year with century
    %Z - Time zone name
    %% - Literal ``%'' character

    t = Time.now
    t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
    t.strftime("at %I:%M%p") #=> "at 08:56AM"

    Monday, May 25, 2009

    Hackintosh X61 (non-tablet) Upgrade to 10.5.7


    Just upgraded my X61 hackintosh to 10.5.7. Upgrading process is easy. Nothing to take note all. No script required to run or delete any hidden kexts. Just use iDeneb 10.5.7 Upgrade Kit. Well done iDeneb team. It is similar to upgrading using Apple combo or delta update pack. Just run the installer and reboot twice and then 10.5.7 is installed.

    To download iDeneb 10.5.7 Upgrade Kit, click here.


    Monday, May 11, 2009

    Rails link_to Previous Page

    There is a useful shorthand in Ruby On Rails.

    :back

    < %= link_to "Back", :back % >

    is equivalent to redirect_to(request.env["HTTP_REFERER"])


    Saturday, May 9, 2009

    Rails Autocomplete Plugin

    To enable Rails Autocomplete Plugin, you need the following steps:

    1. Install plugin:

    script/plugin install auto_complete

    2. Add this line to the controller where auto_complete to be used:

    auto_complete_for :contact, :name

    3. < %= text_field_with_auto_complete :contact, :name, { :size => 10 }, { :skip_style => false,
    :method => :get} % >


    Using :method => :get overcomes the problem with InvalidAuthenticityToken.

    4. To customize the result, you can override a method in your controller.

    def auto_complete_for_contact_name
    @user = User.find_by_id(session[:user_id])
    re = Regexp.new("^#{params[:contact][:name]}", "i")
    find_options = {:conditions => ['company_id = ?', "#{@user.company_id}"],:order => "name ASC" }
    @organizations = Contact.find(:all, find_options).collect(&:name).select { |org| org.match re }

    render :inline => "< %= content_tag(:ul, @organizations.map { |org| content_tag(:li, h(org)) }) % >"
    end


    The example above shows that the result will be filtered by company_id and sorted by name.

    5. To beauty the autocomplete results, the css below can be helpful.

    div.auto_complete {
    position:absolute;
    width:250px;
    background-color:white;
    border:1px solid #888;
    margin:0px;
    padding:0px;
    }
    li.selected { background-color: #ffb; }

    Friday, May 8, 2009

    Using ISAPI Rewrite on Rails App with IIS and Mongrel as Windows Service

    In setting up a multiple production environment in a single server, IIS alone cannot do the redirection of url nicely. When a RAILS app runs in IIS, it requires port number and to redirect domain name to the particular port number, ISAPI Rewrite can help. Below are some sample of how to redirect a hostname to the port required.

    RewriteCond %{HTTP:Host} ^(?:www\.)?domain name\.biz$
    RewriteRule (.*) http://www.domain name.biz:3000$1 [R=301,L]

    RewriteCond %{HTTP:Host} ^(?:www\.)?another domain name\.com$
    RewriteRule (.*) http://www.another domain name.com:3001$1 [R=301,L]

    Updated: 27th May 2009
    Possible to do URL masking by using rewrite proxy

    RewriteCond %{HTTP:Host} ^(?:www\.)?another domain name\.com$
    RewriteProxy (.*) http://localhost:4002$1

    Note: it will map to localhost:4002 and transparent to user as the url shown is www.anotherdomainname.com. You can point multiple domain name to a single web app.

    To get this done:
    1. Install ISAPI Rewrite.
    2. Copy and replace domain name to your own domain name in ISAPI rewrite.
    3. If you have more than one domain pointing to the same server, just add below the first domain.

    Thursday, April 30, 2009

    Calendar Date Select for Ruby on Rails

    CalendarDateSelect is semi-light-weight (about ~20k of javascript/css) and easy to use (but gets even lighter with bundle-fu)! It takes full advantage of the prototype.js library, resulting in less code, but maintaining a great deal of functionality.

    Some of it's capabilities:

    can pick a date, or a date-time, or a date with an optional time, as a popup selector or an embedded calendar!
    speaks several different formats
    is easy to localize
    is extensible. Calendar Date Select doesn't do everything under the sun (which is a good thing), but you can build on top of it.
    Installation / Usage

    Install the calendar_date_select gem
    sudo gem install calendar_date_select
    Add a gem dependency to your rails project (in environment.rb)
    config.gem "calendar_date_select"
    Then, restart/start your rails project. All necessary files will be copied to your public folder.
    Now include this at the top of your layout: (you'll also need to include prototype, if you haven't already!)
    <%= calendar_date_select_includes "red" # or "blue", or "silver", or nil for the default! %>
    # (if you want more control, you can get a list of stylesheets / javascripts via calendar_date_select_javascripts and calendar_date_select_stylesheets)
    See the Demo for various uses of CalendarDateSelect.

    Wednesday, April 29, 2009

    Date Calculation in Rails

    >> Date.new( 2008, 1, 1 ).to_time.advance( :months => 1 )
    => Fri Feb 01 00:00:00 +0000 2008


    >> Date.new( 2008, 2, 1 ).to_time.advance( :months => 1 )

    => Sat Mar 01 00:00:00 +0000 2008

    >> Date.new( 2008, 1, 1 ).to_time + 1.month
    => Thu Jan 31 00:00:00 +0000 2008

    Extracted from here:
    http://roninonrails.blogspot.com/2008/01/beware-of-rails-date-arithmetic.html

    Tuesday, April 28, 2009

    Get Month Name in Rails

    To get month name as constants, do the following

    require 'date'

    for i in (1..12)
    @month = Date::MONTHNAMES[i]
    end

    Date Format in strftime

    FormatMeaning
    %aThe abbreviated weekday name (“Sun'’)
    %AThe full weekday name (“Sunday'’)
    %bThe abbreviated month name (“Jan'’)
    %BThe full month name (“January'’)
    %cThe preferred local date and time representation
    %dDay of the month (01..31)
    %HHour of the day, 24-hour clock (00..23)
    %IHour of the day, 12-hour clock (01..12)
    %jDay of the year (001..366)
    %mMonth of the year (01..12)
    %MMinute of the hour (00..59)
    %pMeridian indicator (“AM'’ or “PM'’)
    %SSecond of the minute (00..60)
    %UWeek number of the current year, starting with the first Sunday as the first day of the first week (00..53)
    %WWeek number of the current year, starting with the first Monday as the first day of the first week (00..53)
    %wDay of the week (Sunday is 0, 0..6)
    %xPreferred representation for the date alone, no time
    %XPreferred representation for the time alone, no date
    %yYear without a century (00..99)
    %YYear with century
    %ZTime zone name
    %%Literal “%'’ character