Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

create object fom underscore_syntax (See related posts)

// camelize - from underscore_syntax to Uppercas/ClassSyntax
// constantize - create instance
// (params[:property]) -- read data from web-form
    @property = property_type.camelize.constantize.new(params[:property])

Comments on this post

mathie posts on Nov 28, 2006 at 14:16
Hmm. Let's DRY that up a little, eh?

class ApplicationController
  protected
  def new_from_params(obj)
    obj = obj.to_s
    klass = obj.camelize.constantize
    instance_variable_set("@#{obj}", klass.new(params[obj]))
  end
end


And then in your controller, call:
new_from_params :property
which would automatically set
@property
with the newly created object from params.

Perhaps that's a little *too* magic though. :-)

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts