Posts Tagged ‘Rails’

Setup Wildcard Subdomain on Localhost for Development Work without /Etc/hosts TomFoolery

Posted 05 Mar 2010 — by admin
Category Programming

Step 1.

Open up your browser and visit http://www.hexxie.com. You can also go to anything.hexxie.com and everything.hexxie.com, which will resolve to your local machine (assuming it’s localhost at 127.0.0.1).

How it works

Super simple. I just pointed hexxie.com and *.hexxie.com to 127.0.0.1, which is everyone’s localhost address. If you’re on Rails just append the port number as usual. http://hexxie.com:3000 or on Django http://hexxie.com:8000. Or you can always fire those guys up on port :80 with sudo ./script/server -p80 for Rails
or sudo django-admin.py runserver 80

To set up your own just configure DNS to point your domain to 127.0.0.1 for the IP address. No more futzing with /etc/hosts

Originally got this tip from http://tbaggery.com/2010/03/04/smack-a-ho-st.html who has created his own service at smackaho.st

The word “Hexe” is German for “Witch”. I have a dog named “Hexxie” after the German word and that is the origin of the domain name hexxie.com, in case you’re wondering.

Extending Rails Form Builders

Posted 01 Mar 2010 — by admin
Category Ruby on Rails

Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so that I can display a more human friendly string to represent the form field.

# RAILS_ROOT/app/helpers/custom_tag_builder.rb
class CustomTagBuilder < ActionView::Helpers::FormBuilder

  def self.create_tagged_field(method_name)
    define_method(method_name) do |label, *args|
      label_name = args.first.blank? ? label : args.first[:label] # my change
      @template.content_tag("p",
        @template.content_tag("label",
          label_name.to_s.humanize, :for => "#{@object_name}_#{label}") +" 
"+ super) end end field_helpers.each do |name| create_tagged_field(name) end end

You can then use this in your views

form_for @your_model, :builder => CustomTagBuilder do |f|
  f.text_field :fullname
  f.text_field :email, :label => "Email (will not be published)"

My change tests for the presence of a label argument otherwise using the name of the form field/model attribute. In this case the fullname attribute will be outputted as “Fullname” while “Email (will not be published)” as the label for the email text field.

Ruby Strftime Day Without the Leading Zero

Posted 20 Feb 2010 — by admin
Category ruby
%e # rather than %d
Time.now.strftime("%e")

Placing an Authenticity Token in a Rails Form

Posted 14 Dec 2009 — by admin
Category Ruby on Rails
 <%= hidden_field_tag :authenticity_token, form_authenticity_token %>

Ruby on Rails, jQuery and YUI API Docs Available as Mac OS X Dictionary Binaries

Posted 05 Dec 2009 — by admin
Category Documentation

I came across an awesome tool this morning. Priit Haamer has chunked Ruby on Rails, jQuery, and some of YUI documentation into native Mac OS X dictionary binaries. This lets you search those API docs from Spotlight, TextMate, any application that uses the dictionary app!

I have tested the Ruby on Rails API within TextMate. Hover over any function and hit ctl+cmd+d and a little popup will give you a glimpse at the API docs. Seems like a nice alternative to ACK or the normal TextMate function lookup.

More information and installation instructions are available at Priit’s site http://www.priithaamer.com/blog

Here is a nice video of the tool

http://www.vimeo.com/3848473

A Through Z

Posted 30 Oct 2009 — by admin
Category Ruby on Rails

How to print the alphabet in Rails very easily.

("A".."Z").each {|letter| link_to letter, "/#{letter"}
"A".upto("Z") {|letter| link_to letter, "/#letter"}

Trouble Using Attr_Accessor in Rails Models and Forms

Posted 07 Aug 2009 — by admin
Category Ruby on Rails

You might use the attr_accessible method to create getters and setters for a class that has attributes which don’t map directly to corresponding fields in a database. For example let’s take the scenario where you are processing a credit card transaction. You don’t want to save the credit card details, such as card number and verification value etc, however you still want to use these attributes in a form and you want to perform validations on them.

class CreditCardPurchase < ActiveRecord::Base
  attr_accessor :number, :cvv
end

The only probem that I've run into is using the attr_accessor on datetime select form fields. Rails won't be able to determine the "klass" and will spit out a nasty error. Looks there is some discussion around this bug. I've got another post about this topic http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/ but unfortunately not a whole lot of resultion :(

Make Rails Lib Module Methods Available to Views

Posted 22 Jul 2009 — by admin
Category Ruby on Rails

If you create a module in the lib/ directory of your Rails application you won’t have access to those methods in your views. If you don’t want to put those methods in a helper file, you need to add a method to your module that makes them available for you views like so…

#in lib/my_module_name.rb
module MyModuleName
  def my_method_for_views
     #logic...
  end
  def self.included(base)
    base.send :helper_method, :my_method_for_views if base.respond_to? :helper_method
  end
end

Fixing MySQL for Rails 2.2 Development on Mac OS X

Posted 03 Jul 2009 — by admin
Category Programming

Oh what trouble Rails 2.2 and MySQL (on Mac OS X) can be. Rails, as of version >= 2.2, no longer comes bundled with the MySQL adapter. This means you’ll need to install it yourself, but it appears that the gem for installing it is also broken.

This will fail

gem install mysql

What you need to do is tell the gem which MySQL to use. I installed MySQL with mac ports, http://macports.org , so I need to specify this when I run the installation command.

gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

Check out an earlier post explaining how to install MySQL with mac ports http://seanbehan.com/programming/installing-sphinx-search-engine-on-mac-os-x-or-ld-library-not-found-for-lmysqlclient/

You’re not out of the woods yet. Rails needs to know the location of the socket it uses to connect to the MySQL server. I created a symbolic link to the location Rails normally looks for this socket. You could specify the location of the socket in the config/database.yml file, however, then you’ll need to do this with each of your applications.

ln -s /private/tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock

If there is an easier way to get MySQL back for Rails on Mac OS X please let me know. I’d love a quick fix to this problem.

Using Your Partials in Your Liquid Templates

Posted 11 Jun 2009 — by admin
Category Ruby on Rails

I’m working on a project that requires users/designers be allowed to edit the layout of their site. I’m using Liquid, Ruby templating system developed by the folks at shopify.com.

Doing a little searching I found this great post http://giantrobots.thoughtbot.com/2008/10/3/custom-tags-in-liquid The liquid documentation itself needs a little more work. But you can check it out here if you want http://wiki.github.com/tobi/liquid/liquid-for-programmers.

I worked through the example at thoughtbot blog and was having trouble with the final step, rendering the partial to the screen. The solution was really a simple step I wasn’t thinking about. Nonetheless, the information for some reason was kept out of the post and burried in the comments.

<%= Liquid::Template.parse(template).render({}, :registers=>{:controller => controller} %>