Category → rails
April 2012 Meetup Videos
Friendster hosted this month’s meetup.
Turnout wasn’t as good as last month’s, but that only meant

more pizza and booze for us!
How to Use jQuery For Rails 3
jQuery is the defacto Javascript framework used by happy programmers. I am very glad to use it in almost all of the applications I am working on.
There are many posts on how to use it for Rails 3. But here's another one which may help Rails 3 newbies (aren't we all newbies when it comes to Rails 3?).
The jQuery Rails gem
In your Gemfile, include the jquery-rails gem created by Andre Arko of Engine Yard.
gem "jquery-rails"
And install the gem using the command:
bundle install
Install jQuery. The following command will remove prototype and scriptaculous files and add jQuery to your application:
rails generate jquery:install or rails g jquery:install
Using livequery
So you probably know that the following code will no longer work:
<= link_to_remote 'delete', :url => admin_image_path(image), :confirm => 'Are you sure you want to delete this image?', :method => :delete >
They probably realized there's way too much javascript on the body tags making Rails apps look ugly when you view the source.
There's a need to include livequery within the head tag or before closing body tag if you wish to follow the YSlow recommendations.
On application.js
$('a[data-remote=true]').livequery('click', function() {
return request({ url : this.href });
});
$('form[data-remote=true]').livequery('submit', function() {
return request({ url : this.action, type : this.method, data : $(this).serialize() });
});
$(function() {
$(".alert").click(function() {
alert(this.getAttribute("data-confirm"));
return false;
})
})
Replace the link_to_remote code with the following:
<%=link_to "Delete", admin_image_path(image), :method=>:delete, :confirm=>"Are you sure you want to delete this image?", :remote=>:true, :class=>"alert"%>
That is it! Advantages of this code over the previous code is obvious. It's not obtrusive and it works. Congratulations to all of us. We're no longer living in caves and eating whatever exists in the wild. We actually have a choice.
This post by Katz Pe is from BridgeUtopia.
Ruby on Windows
Some posts just write themselves. Today’s post comes from my reply to a guy in PhRUG who still thinks you need a Mac before you can develop Rails applications.

The biggest problem the Ruby/Rails community has when trying to spread the word in this country: the lack of interest in supporting Windows.
I mean, a typical response to the legitimate question “I’m using Windows, how to I practice RoR?” is the fanboy answer: “Get a Mac!”
And that, my dear readers, is a dick move. If I was an average college student and you told me that, I’ll immediately think “WTF?!? I just want to try out this open-source language and web framework and I need to shell out a couple of years worth of tuition?!?”
Answering “Format your hard drive and install Linux” is less of a dick move, but a dick move nonetheless.
Thus, if we rubyists want to spread the word about Ruby, we’ll have to make Windows a viable OS for Ruby development. Here are a few options available to us:
Use Ruby 1.9.2
Startup speed is probably the main problem with developing Rails in Windows. Fortunately, Ruby 1.9 is a heck of a lot faster than 1.8 thanks to the new interpreter.
I tried to install Rails 3 on my gaming rig just now to see the difference.
- Downloaded and installed the 1.9.2 installer from the official ruby site. Note that you must tick the Add Ruby executables to your path to remove the need for doing that later.

- Went to the command prompt (I used PowerShell because it’s just better) and installed Rails 3 using the same command for installing it in Linux or OS X:
gem install rails --no-ri --no-rdoc

- Downloaded SQLite3 Windows DLL from the official download page and extracted it to the Ruby bin directory (use
gem envto determine the path)

- Created the 6-command blog program. (
cd blognot included in pics)





- Opened http://localhost:3000/entries in a browser. Works as expected.

Conclusion: Ruby 1.9 on Windows is pretty fast. Have the newbie install this along with NetBeans or RadRails and he should be diving into Rails in no time.
Virtualization (my personal choice)

The problem with Windows is that some important gems (rvm, unicorn, and passenger) are not supported by the OS. Fortunately, with a decent computer, you can run Linux inside of Windows without having to dual boot. VirtualBox or VMWare Player are both free virtualization options available on Windows.
For a gaming level rig (~4 GB RAM, RAM is pretty cheap nowadays), installing Ubuntu Desktop 10.4 (Lucid Lynx) on a VM with 10GB of space and 1GB of RAM should be more than enough for development needs. If you want to use a Windows editor/IDE to edit your files, you can setup shared folders to gain access to the files from the host PC.
For a slightly weaker machine, you could install Ubuntu Server on a VM with 10GB of space and 512MB of RAM, and voila! instant VPS on your PC. You can even use openssh-sever + PuTTY to emulate having multiple terminal windows.
JRuby
Pretty much the same as MRI Ruby with some minor differences. We don’t have rvm on Windows so you’ll have to change all of your “ruby” commands with “jruby” and prefix your ruby system commands with “jruby -S” e.g. “jruby -S gem ...“.
Downside is that it has the slow startup speed of 1.8 (they say you could tweak it to preload the libraries faster, but I haven’t tried it myself) and the native extension problems of having a different environment.
On the plus side, it’s on the JVM: just package your project into a WAR file with Warbler and it should be deployable on any J2EE application server. Great for convincing J2EE software houses to try out Rails.
–
In closing, if you’ve got a MacBook Pro and someone asks you about developing Rails on a PC, don’t be a dick and tell them “Get a Mac!”. Ruby is just fine regardless of whether you’re using Windows, Linux, or OS X.
This post by Bryan Bibat is from existence, refactored.
Install RVM, Passenger, Nginx and Rails 3 on Ubuntu Lucid Lynx
This has been a problem encountered by several developers including myself:
Getting RVM, Passenger and Rails3 to work
Here's a quick and short post. Supposing you already have updated Ubuntu and have Ruby installed. These are the only commands you should need:
rvm install 1.9.2 rvm 1.9.2 --passenger rvm 1.9.2 gem install passenger rvmsudo passenger-install-nginx-module #use rvm by default rvm 1.9.2-preview3 --default #edit the nginx config file. mine is on /opt/nginx/conf/nginx.conf. just change username passenger_root /home/username/.rvm/gems/ruby-1.9.2-preview3/gems/passenger-2.2.15; passenger_ruby /home/username/.rvm/bin/passenger_ruby; #reboot if you can. I recommend rebooting over restarting/stopping and starting nginx server. reboot was actually the fix I needed after following all the steps above. sudo reboot #.rvmrc file on your Rails 3 app should contain. this may be optional but in case you've set a different ruby version, this helps. if [[ -s "~/.rvm/environments/ruby-1.9.2-preview3" ]] ; then . "~/.rvm/environments/ruby-1.9.2-preview3" else rvm --create use "ruby-1.9.2-preview3"
If nothing works for you. Check your path, your ruby version and check whether bundler is installed.
How to installed bundler for Rails 3:
gem install bundler --pre
Most gems for Rails 3 require "--pre" by the way.
Installing gems
cd app/dir && bundle install
It's that simple. But something failed. I use mysql and pg gem. I use mysql gem for test and development environment because they work better for testing. But I use postgresql for production and staging. Mysql gem was not installed by bundler. You have to run this:
gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
Hope this helped someone.
This post by Katz Pe is from BridgeUtopia.
Recent Comments