Monday, August 24, 2009

Drop downs selections for belongs_to in rails

Rails does a lot of work for you out of the box, and with it's form builders you can get it to do some things that make you never want to go back to .NET and databind a control ever again...

Example: Recently I needed to bash together a quick admin screen with a couple of models. We have an organisation model that belongs_to :region and a Region model that has_many :organisations. This dictates a foreign key in organisation that points at a single region. In our generated Rails forms we enter that as a number, but it'd really be a nicer user experience to select the region from a drop down list right?

Enter collection_select. This little helper generates a selection dropdown from a collection (not surprisingly). So if in my organisation_controller's edit method I return @regions = Region.all we can get instant rails gratification.

Finally, since Edit and New use EXACTLY the same form, we're going to try be DRY and pull all the duplicated code out into a partial. I usually use the default form builder to base it on (you know, "form_for(@organisation) do |f|" ) and call "render :partial => f"

When this renders it looks for the partial pointed at by f, which is a form. so _form.html.erb will be rendered. A final note, even though the partial here will be able to see @regions, I like to explicitly pass it in the locals hash, so I know where the heck it came from in 6 months time. If that's confusing you can just strip out that code and use the global set in the organisation controller. Here's the code:


class Organisation < ActiveRecord::Base

  belongs_to :region



class Region< ActiveRecord::Base

  has_many :organisations



class OrganisationsController < ApplicationController

  # GET /organisations/new

  # GET /organisations/new.xml

  def new

    @organisation =

    @regions = Region.all

    respond_to do |format|

      format.html # new.html.erb

      format.xml  { render :xml => @organisation }



  # GET /organisations/1/edit

  def edit

    @organisation = Organisation.find(params[:id])

    @regions = Region.all



<%= form.error_messages %>


  <%= form.label :org_name %><br />

  <%= form.text_field :org_name %>



  <%= form.label :org_code %><br />

  <%= form.text_field :org_code %>



  <%= form.label :provider %><br />

  <%= form.text_field :provider %>



  <%= form.label :region_id %><br />

  <%= collection_select(:organisation, :region_id, regions, :id, :full_name, {:prompt => false}) %>



<% form_for(@organisation) do |f| %>

  #:region => regions exposes the regions array as a local variable in the partial

  <%= render :partial => f, :locals => { :regions => @regions } %>


  <%= f.submit 'Update' %>


<% end %>

Monday, August 17, 2009

SOAP4R clients with proxy and basic authentication

If you go and google "soap4r proxy" you'll get a lot of helpful hits that show you how to set your http_proxy environment variable. Unfortunately there's not many (read none) that are devoted to configuring it programatically. So here it is.

If you run the wsdl2r tool to generate code you'll end up with a defaultDriver.rb file. This defines the RPC driver that you can call the webservices against. You'll want to ensure that it's going to use the HTTPClient libraries. If you installed it manually you should be fine, but if you installed it with rubygems you'll need to add requires rubygems to the top of your file.

Next you'll need to add the basic authentication configuration and the proxy configuration to the SOAP driver (using some horrendously poorly documented features of the SOAP4R library). The best place to do this is in your initialize method. You'll end up with a class that resembles the following (I generated mine from a WSDL pertaining to sending SMS messages):

require 'rubygems'
require 'xsd/qname'
require 'httpclient'
require 'soap/rpc/driver'

class MessagingService < ::SOAP::RPC::Driver

DefaultEndpointUrl = ""
MappingRegistry =

Methods = [
["https://somesoapgeneratedmappingname", "sendSMS"),
[ ["in", "to", ["::SOAP::SOAPString"]],
["in", "application", ["::SOAP::SOAPString"]],
["in", "message", ["::SOAP::SOAPString"]],
["in", "options", ["::SOAP::SOAPInt"]],
["retval", "sendSMSReturn", ["::SOAP::SOAPString"]] ],
{ :request_style => :rpc, :request_use => :encoded,
:response_style => :rpc, :response_use => :encoded }

def initialize(endpoint_url = nil)
endpoint_url ||= DefaultEndpointUrl
super(endpoint_url, nil)
self.mapping_registry = MappingRegistry
self.options["protocol.http.basic_auth"] << [endpoint_url,'username','password']
self.options["protocol.http.proxy"] = "http://yourproxyserver:8080/"


def init_methods
Methods.each do |definitions|
opt = definitions.last
if opt[:request_style] == :document
qname = definitions[0]
name = definitions[2]
if != name and == name.capitalize
::SOAP::Mapping.define_singleton_method(self, do |*arg|
__send__(name, *arg)