Hulihan Applications
about projects portfolio services contact_us

Posts tagged Ruby on Rails


Installing Custom Gems on a Shared Host

    

Have you ever wanted to install any gem you want on your account with a shared hosting company, like hostmonster, bluehost, dreamhost, railsplayground, etc? Here's how you can do it!

 

Before we begin, you'll need ssh access to your account. Log into to your ssh now!

 

First, we must create a directory you can install local gems to

cd ~
mkdir .gem bin lib src

Next, install your own local executable of rubygems. This step is optional, but I like to have the latest version of gem on my account, so I can update it as needed. At the time of this post, the latest version of rubygems is 1.3.7.

cd ~/src
wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
tar xzvf rubygems-1.3.*
d rubygems-1.3.*
ruby setup.rb --prefix=$HOME
cd ~/bin
ln -s gem1.8 gem
cd ~

Next, add this  to the your user's .bash_profile file(~/.bash_profile), so the gem command knows where to install new gems:

export PATH="$HOME/bin:$HOME/.gem/bin:$PATH"
export RUBYLIB="$HOME/lib:$RUBYLIB"
export GEM_HOME="$HOME/.gem"
export GEM_PATH="/usr/lib/ruby/gem/1.8:$GEM_HOME"
alias gem="nice -n19 ~/bin/gem"

After this step is completely, re-login to your account to apply these changes.

Next, verify that the gem command you're using is in the right place:

which gem   # should return /home/USERNAME/bin/gem
gem -v      # should return 1.3.7

 

That's pretty much it! You can now install gems locally to your account using the command gem install [gemname].

Now, if your application needs access to a gem that's installed locally to your account, add this to your application's config/environment.rb or config.ru(if you're using a rackup config file).

That's it!

Special Thanks goes to Katz for her guide on installing gems on dreamhost.

 


  1 Comments


Use Scriptaculous To Toggle or Replace HTML

     Scriptaculous is a visual effects Library for javascript. It's used quite a bit because it comes with all ruby on rails applications by default. In our upcoming Item Management application, Opal, we use scriptaculous quite a bit to make boxes and other html items do interesting things. Here's an example of how you can use scriptaculous to toggle(turn on/off) an item, and also how you can replace one item with another with a cool visual effect.

Assuming we've just started a new webpage, here's what we do:

First We Need to Load the Scriptaculous & Prototype Library:
	
	
The Prototype Library is also required to run Scriptaculous.

Then We'll add our Javascript Functions that will control how Scriptaculous will work with our objects:
	
By the way, these javascript functions are designed to be customizable, so you can change the effect according to your taste. For example, if I wanted the box to shrink down instead of fade out, I would change line 26 to:
	new Effect.Shrink(element, {duration:0.5}); // Hide Element(Must be an Appearing effect listed above)
Just be sure not to do use an appearing effect instead of a disappearing effect, otherwise weird things will happen. Let's move on.

Now we'll create the object that is hidden on the page. Note the id is my_box.
	

This is very cool!


Now we need to create a link(can be a hyperlink, image, anything, etc.) to trigger the effect. Notice how we tell it the id of the object to toggle.
	Toggle Box!
That's all there is to it! Give it a try: Toggle Box!

This is very cool!





Also, you can replace one object with another using the code provided above.

Add the two objects, notice that one is hidden and the other is not.
	

This is very cool!

Now we add the link that will trigger the switch. Notice that we have to pass in the ids of both objects. Also, notice how one of the boxes is hidden(with css's display:none property).
	Replace Box! - 
	Replace Opposite Boxes!

Will it work? Let's Give it a try: Replace Box! - Replace Opposite Boxes!

This is very cool!



  0 Comments


Pass a variable to a partial in Ruby on Rails

    Using partials in ruby on rails is one way to use code from different files on one page. They are very handy when you need to use the same bit of code over and over in a site or application. Example: Let's say you want to show a listing of different users, and you want that listing to look the same, you could use a partial to keep the format of the list the same no matter how you're viewing the list of users. The list would look the same if you're searching users, listing them alphabetically, etc.

Partials are great, but you have to specify which objects and variables will be available to them. This is different than including a file in PHP, for instance. In PHP, when you include code from a different file, the entire page is executed as if the code you're grabbing from a different file is already on the page.

However, in ruby on rails, you have to pass the variable to the partial, otherwise the code in the partial won't see any variables on the main page. So here's an example of how you can pass a variable to a partial:

main_page.rhtml
	

Listing Users

<% users = User.find(:all) %> <%# Pass the users into the partial %> <%= render :partial => "user_list", :locals => {:users => users} %>
This will grab code from:

_user_list.rhtml
	<% for user in users %>
	 <%= user.name %>
<% end %>
That's all there is to it!

  2 Comments


DiamondList Released!

     Hello Again! Just Letting everyone know we released DiamondList today, our Ruby on Rails wishlist application. It's a wishlist application that allows you to track items and gifts that someone would want for a special occasion(christmas, birthday, wedding, etc.). Just in time for Christmas!

This application is free and open source, so you can change it however you want. It's very useful for big families that want to see what another person in the family wants. Also, you can mark the status of a gift/item on a list, so you can see if someone else has already bought the item. Very handy. If you want to download it or try out a live demo, here's the address of the project page: Give it a try, and let us know what you think!

  0 Comments


Install Aptana and RadRails in Eclipse

     For those of you who are interested in web development, a good IDE(Integrated development environment) can save you time and stress. For ruby on rails development, I usually enjoy using Aptana with the RadRails plugin. RadRails is a Ruby & Ruby on Rails development environment that has useful ruby code snippets and code highlighting. Also, it works with html, css, javascript, etc. So it's very useful for web development. I particularly enjoy Aptana(which is a derivitive of the popular Java IDE, Eclipse) because it allows me to use FTP file browsing, so I can connect directly to a development or web server, no matter where I am or what machine I'm using for development.

I have been using Aptana for a while with no problems, on multiple machines. Everything was going great, that is, until I tried installing it on my new laptop. Every time I tried to open the program, I received a "JVM Terminated" Message Window, and Aptana immediately closed. The guys at Aptana say the culprit of this message is caused by a problem with JVM on my machine. After reinstalling, updating and verifying that JVM is working on my machine, I tried it again. Same thing! It also seems that other people with this same issue had unresolved support tickets for Aptana's Development Team. So far no one had resolved this particular issue, and I needed a quick solution. I decided to install Aptana and RadRails as an Eclipse Plugin. Here's how to do it:
Step 1: Download and Install Eclipse
You can download Eclipse here. Be sure to grab an SDK version of Eclipse. On my machine, I used the basic Eclipse Classic SDK IDE(Ganymede), version 3.4.1. Follow their instructions and install Eclipse as necessary.
Step 2: Prep Eclipse to Install Plugins
Open Eclipse, then:
1) From the Window or Eclipse menu in Eclipse, select Preferences.
2) Select General > Capabilites, and check the Classic Update box.
3) Click OK(The Main OK for the entire box).
Step 3: Install The Aptana Plugin
1) From the Help menu in Eclipse, select Software Updates > Find and Install... to open an Install/Update pop-up window.
2) On the Install/Update pop-up window, choose the Search for new features to install option, and click the Next button.
3) Add a new remote site to add the Aptana plug-in:
1. Click the New Remote Site... button to open a New Update Site pop-up window.
2. On the New Update Site pop-up window, type Aptana in the site Name text box.
3. In the URL text box, type the URL for the Aptana update site: http://update.aptana.com/install/studio/3.2/ and click OK.
4. Click the Finish button to switch to the Updates window.
4) On the Updates window, check the Aptana box (shown below), and click the Finish button.
5) On the next screen, check the Aptana box, and click the Next button.
6) Choose the option to accept the terms of the license agreement, and click the Next button.
7) Click the Finish button.
8) Click the Install All button.
Step 4: Install The RadRails Plugin
1) Follow the steps above, but In step 3, Name the plugin "RadRails", and use the URL: http://update.aptana.com/install/rails/3.2/
Step 5: Relax
That's all you need to do to get Aptana Running in Eclipse. It's free and useful. Also, Eclipse has SFTP support, so your passwords won't be displayed in cleartext(they are in normal FTP) when connecting to a server.
Resources used in this post:
Plugging Aptana into an existing Eclipse configuration - A great article with several screenshots. It covers steps 1-3 above, but I had to dig around to find the plugin url for RadRails in step 4.

  6 Comments


Downgrading Ruby on Rails

    

        Well, now that the new version of ruby on rails is out(2.0.2), everything should be new and improved. I do like a lot of things in the new version of rails but many of my apps are written in previous versions(like 1.2.6) and I'd like to keep them that way, for now. Rails 2.x is pretty good, but it's missing a lot of old rails core commands(like the old rest-less script/generate scaffold), that I happen to use sometimes. Also, a lot of people deploy rails applications in shared hosting environments and other places where they don't have the permissions to reinstall old versions of rails via gem, so here's an alternative. Here we will actually freeze the older version of rails to our application. In this example, I'll be downgrading ruby on rails 2.0.2 to 1.2.6. 

First, create a new rails application(we'll call it app):

     $ rails app

Then, cd into the directory:

     $ cd app

Now we will download the version of rails you want(here we will grab 1.2.6, but you can get any version) from the rails archive:

Note: you can do this a couple of different ways, this is using svn

     $ svn co http://dev.rubyonrails.org/svn/rails/tags/rel_1-2-6/

Copy and rename the downloaded folder to vendor/rails: 

     $ mv rel_1-2-6/ vendor/rails

Note: Rails looks automatically for a vendor/rails folder before using the server-wide ruby on rails libraries(aka gems). Having them stored this way is called "freezing" your gems.

Now all we have to do is replace the 2.0.2 version of  config/environment.rb file with a copy of the 1.2.6 version:  

     $ mv config/environment.rb config/environment.rb.original

     $ nano -w config/environment.rb

Here I'm using the linux text editor, nano. You can also use emacs, vi, notepad, textpad, or whatever you want. Basically, we're going to create a new empty file named config/environment.rb and paste this code into it:

ENV['RAILS_ENV'] ||= 'development'
RAILS_GEM_VERSION = '1.2.6'

require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
  config.action_controller.perform_caching = true
end

If you used the nano command(which I am using in this example), you can save the file by pressing CTRL + O(hold down the ctrl key and hit O), and then CTRL + X. 

That's it! You can now confirm that your using rails 1.2.6 by running this command:

      $ script/about

 You should see the version of rails now being 1.2.6, like so: 

About your application's environment
Ruby version                     1.8.6 (x86_64-linux)
RubyGems version             1.0.1
Rails version                            1.2.6
Active Record version          1.15.6
Action Pack version             1.13.6
Action Web Service version  1.2.6
Action Mailer version           1.3.6
Active Support version         1.4.4
Edge Rails revision             8976
Application root                  /home/bob/app
Environment                      development
Database adapter              mysql

 

 


  1 Comments


Mysql Console Rake Task

    

    I just wanted to share a handy rake task that I found. It allows you to connect directly to a mysql console in a linux shell(granted the 'mysql' command is present). It reads in the mysql connection settings stored in config/database.yml so there's no need to enter in a user/database name or password. Here's the code you can add to lib/tasks/mysql.rake (or whatever) or Rakefile

def mysql_console(config)
  returning '' do |mysql|
    mysql << mysql_command << ' '
    mysql << "-u#{config['username']} " if config['username']
    mysql << "-p#{config['password']} " if config['password']
    mysql << "-h#{config['host']} "     if config['host']
    mysql << "-P#{config['port']} "     if config['port']
    mysql << config['database']         if config['database']
  end
end

def mysql_command
  'mysql'
end

desc "Launch mysql shell.  Use with an environment task (e.g. rake production mysql)"
task :mysql do
  system sh_mysql(YAML.load(open(File.join('config', 'database.yml')))[RAILS_ENV])
end

 Then you would run the task by typing:

 $ rake mysql

 Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 109184 to server version: 5.0.45-community-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

That's it!


  0 Comments


attr_accessor and ruby object variables

    

    In most other languages(java, c++, etc.), when you create an object, you have to define the variables that will make up the structure of the object. You also usually add a getter(retrieves the variable) and a setter(sets/writes the contents of the variable) so you can access the variable. 

However, in Ruby, you only need one line of code that will do all of the above:

attr_accessor :myvar

This creates the getter and setter methods from the variable, which(by default) look like this:

   def myvar #this is the getter

           @myvar #returns the contents of @myvar

        end

        def myvar(anything) #this is the setter

           @myvar = anything #set the contents of myvar to anything!

        end

You're also looking at the code to override the methods, too!  Example: If you want to create a backup variable whenever you create the original, do this:

app/model/example.rb 

Class Example

        attr_accessor :myvar 

        def myvar=(anything) #this is the setter

           @myvar_backup = @myvar #backup the original variable

           @myvar = anything #set the contents of myvar to anything! 

        end

 end

script/console 

@myExample = Example.new
=> #<Example:0x2b50b2c3d560>
>> @myExample.inspect # see what the object has
=> "#<Example:0x2b50b2c3d560>"
>> @myExample.myvar = "hello!" # set the variable
=> "hello!"
>> @myExample.inspect # see what the object has now
=> "#<Example:0x2b50b2c3d560 @myvar_backup=\"hello!\", @myvar=\"hello!\">"

Also, if you just wanted the getter or setter only, you would use:

attr_reader :myvar or attr_writer :myvar

 

 Read more about accessors: 

http://www.ruby-doc.org/docs/UsersGuide/rg/accessors.html


  3 Comments




Hulihan Applications © 2007-2009
No portion of this site may be copied, altered, duplicated or otherwise used without the express written approval of Hulihan Applications.