Ruby on Rails Snippets & Howto's

Collection of Ruby on Rails related snippets and howto's that I find interesting enough to share with you guys. 

Install nokogiri and libxml on ubuntu

Solves all the dependencies libxml2 errors.

sudo apt-get install libxml2 libxml2-dev libxslt1-dev
sudo gem install nokogiri

Filed under  //   nokogiri   ubuntu  

Comments [1]

Liquid error: undefined method `to_liquid' for #<Class:

Screen_shot_2010-04-20_at_11

I was just playing around with liquid markup for Ruby on Rails. Liquid markup is a great template engine created by the folks behind Shopfiy. Liquid allows users to create their own templates, but prevents them from running insecure code on your server. It also allows you to store templates in a database, which is a big plus.

Generally speaking Liquid Markup denies all access to normal variables and functions. Using Drops and Filters you give your users access to functions and pieces of information.  

The only big downside is that there is little information available about using Liquid Markup. Don't get me wrong, there is enough information if you google it, but for the size of the project and capabilities it is too little. 

Therefor when I ran into the error above and solved it, I thought it might be useful to share it with you guys.

What I was trying to do is generate a menu tree in liquid. I used the acts_as_nested_set plugin to manage the tree. It has a handy function (self_and_siblings) for collecting the active menu item and it's siblings.

I created a Drop containing the function:

class PageDrop < BaseDrop

  def initialize(page) 
    @page = page 
  end

  def self_and_siblings
     @page.self_and_siblings
  end

end

and the liquid code to build the tree:

<div id="menu">
  <ul>
    {% for page in page.self_and_ancestors %}
    <li><img src="/images/li_icon.png">{{ page.title }}</li>
    {% endfor %}
  </ul>
</div>

it then generated the following error:

Liquid error: undefined method `to_liquid' for #<Class:0x102aa78e8>

After some googling and debugging the code I noticed the error was produced because acts_as_nested_set returnes class instances and liquid extensions (for security reasons) don't support class instances. 

Modifying 

     @page.self_and_siblings

to

     @page.self_and_siblings.to_a

quickly solved the problem, while maintaining the security.

If you ever walk into this error, I hope the above will help you out.

Filed under  //   liquid markup   ruby on rails  

Comments [2]

Time Zones in Rails 2.1 in under two minutes

Pastedgraphic-1

In the past, time zones have been very difficult to work with, but no longer! Rails 2.1 brings with it great time zone support.

I got timezones working in under two minutes using Ryan Bates screencast and this blog post.

Filed under  //   ruby on rails   timezones  

Comments [0]

12 Ruby on Rails Screencasts

A fun way to learn more about Ruby on Rails techniques or plugins is by watching screencast tutorials. Screencasts are short movies covering a specific problem or technique. They are very useful if you are planning on building something with a technique you haven’t used before.

Let’s say you are planning on integrating PayPal into your rails app. Railscasts has a clear screencast on that topic, giving you a runthrough and a example of the entire process needed for ingegrating PayPal. 

Read the rest of this post »

Filed under  //   ruby on rails   screencasts  

Comments [0]

Rmagick on Snow Leopard

Screen_shot_2010-04-01_at_10

Thanks to Sheldon's tip on refreshrate.com I got Rmagick and paperclip up and running on my Snow Leopard installation.

First install ImageMagick using port

sudo port install ImageMagick

Next go to this directory

cd /usr/local/lib

Backup these two dylib files

sudo mv libexpat.1.dylib libexpat.1.dylib.original
sudo mv libexpat.dylib libexpat.1.dylib.original

Copy dylib files from Developers package

sudo cp /Developer/SDKs/MacOSX10.6.sdk/usr/lib/libexpat.1.dylib .
sudo cp /Developer/SDKs/MacOSX10.6.sdk/usr/lib/libexpat.dylib .

Now gem install rmagick will run smoothly. 

You might need to add a file to the initializers directory

/config/initializers/paperclip.rb 

containing this line:

Paperclip.options[:command_path] = "/opt/local/bin"

It will tell Paperclip where to find the installed ImageMagick bin files

Filed under  //   rmagick   ruby on rails   snow leopard  

Comments [0]

Rails Skeletons


Screen_shot_2010-03-30_at_3

A great way to jumpstart your Ruby on Rails project is to use a skeleton or rails template. On github you can find several projects pre-populated with essential plugins and layouts.

Below you will find some of the best Ruby on Rails skeletons on Github:

Bort

Bort is a base Rails 2.2 application that makes creating new projects easier and faster. Bort is developed and maintained by Fudge Studios, Jim Neath and Matt Hall

- RESTful Authentication
- User Roles
- Will Paginate
- Exception Notifier
- Asset Packager

Big Old Rails Template

This template assists in spinning up new Rails applications quickly using Rails 2.3.

*  Authlogic  for user authentication, including password resets, anonymous_only, authenticated_only, and admin_only application helpers. Optionally installs user activation support.
* World’s simplest authorization system: manage multiple string roles on users with User#add_role, User#remove_role, User#clear_roles, and User#has_role?
* Date formats: :us, :us_with_time, :short_day, :long_day
* Paperclip for attachment management
* /pages/css_test will show most CSS styles in action
* Searchlogic for magic named scopes and search forms. Includes attribute_equals, attribute_does_not_equal, attribute_begins_with, attribute_like, attribute_ends_with, attribute_greater_than, attribute_null, attribute_blank, etc., etc.
* Stringex for extra string functionality – acts_as_url, String#to_ascii, String#to_html, String#to_url, String#remove_formatting, String.random
* US State application helpers
* will-paginate for pagination
* annotate to annotate models, exemplars, and routes
* carmen to handle state and country selects
* rake doc:diagram tasks for use with railroad

Blank

Blank is GiraffeSoft’s starter Rails application.

It makes heavy use of our standard toolset. Controllers are mostly written using ResourceController, and it is tested entirely with Thoughtbot’s Shoulda. Integration testing is taken care of by Cucumber.

- active_presenter
 - andand
 - hoptoad
 - mocha
 - rake
 - restful_authentication
 - ruby-openid
 - will_paginate

BaseJumper

There are others, I know, but this is how I like my apps to start out. You probably won’t like it, so move along.

*  Authentication (including password reset)
* Welcome page placeholder
* Contact Us form
* Basic styling using Blueprint CSS
* Spec driven development using rspec, without fixtures
* ActiveScaffold admin interface
* Application configuration file
* Less CSS for mixing in style
* JQuery and JQuery-UI installed
* FamFamFam icons where needed

Filed under  //   github   ruby on rails   skeleton   templates  

Comments [0]

Installing Ruby on Rails, Passenger, PostgreSQL, MySQL on Snow Leopard

20100208-1up5s7ahybmryrerrxxwn

I recently upgraded to Leopard Snow andfinally broke all my ties with locomotive. I have always developed using locomotive bundles, but since I recently started to deploy more and more rails applications on apache and passenger, it made more sense to also switch my development environment to that setup. 

This article is the best I found on giving you an accurate description on setting up a user-friendly Ruby on Rails, Passenger, PostgreSQL, MySQL on Snow Leopard system.

Filed under  //   passenger   ruby on rails  

Comments [0]

Ruby on Rails Book Club

Screen_shot_2010-03-09_at_11

Jumpstart your RoR knowledge! At rubybookclub.com you will find reviews on all the latest Ruby on Rails books. Books that are fun to read and books that will surely give you a head start on development.

Read more

Filed under  //   REST   programming   ruby on rails  

Comments [0]

CSV Export

Screen_shot_2010-03-09_at_10

A common requirement from customers is the ability to export tabular data to a CSV file that can be imported into Excel. This snippet shows you how to easily export CSV using the standard Ruby CSV library. No extra plugins required.

Read more >>

Filed under  //   csv   ruby on rails  

Comments [0]

Not-so-basic authentication

0picture_3

authenticate_or_request_with_http_basic makes it really easy for you to implement fast and simple authentication. 

The most common use is only 5 lines:

def authenticate
  authenticate_or_request_with_http_basic do |user, password|
    user == 'admin' && password == 'pass'
  end
end

That's all well and good ... until you want to add a log out button. The browser stores the successful login credentials in a sort of cookie, and applies them to every page which requests basic authentication. Once you’ve logged in, it’s actually quite hard to make the browser forget you until you quit and restart the browser. It’s hard, but not impossible. If you can force basic authentication to fail, the browser will throw away the credentials.

Under the hood: Not-so-basic authentication shows you how to solve that issue and more.

Filed under  //   authentication   ruby on rails  

Comments [0]