Sunday, February 07, 2010

Using Formtastic Without ActiveRecord

Let's start with a model:
class PrimaryLanguage
  attr_accessor :name
  def initialize(name)
    @name = name
  end
end
Then create an object in a controller:
class AbcController < ActionController::Base
  def aaa
    @primary_language = PrimaryLanguage.new('English')
  end
end
Design a form:
<% semantic_form_for @primary_language, :url => select_primary_language_path, :html => { :id => 'primary_language'} do |form| %>
  <%= form.input :name, :as => :select, :collection => @languages, :required => false, :label => "Primary Language:<br/>", :include_blank => false %>
<% end %>
And finally handle the user's input;
class XyzController < ActionController::Base
  def bbb
    primary_language_name = params[:primary_language][:name]
    # do something
  end
end
Post a Comment