Lightly tested:

class Parent
  def self.standard_create(name)
    upper_name = name.to_s.capitalize
    klass = Kernel::const_get upper_name
    define_method(:create) do
      ret = instance_variable_set("@#{name}", klass.new(params[name]))
      if ret.save
        flash[:notice] = "#{upper_name} successfully created."
        redirect_to :action => 'index'
      else
        render :action => 'new'
      end
    end
  end

  def flash() (@flash||={}) end
  def params() (@params||={}) end
  def redirect_to(what) puts "Redirecting to #{what[:action]}"  end
  def render(hash) puts "Rendering #{hash[:action]}" end

end

Test2 = Struct.new :params
class Test2
  def save
    puts "Saving with #{params}"
  end
end

Only the standard_create method is important, the result is just fluff
to emulate some of Rails and show its usage.

On 6/25/06, Tony Mobily <merc / mobily.com> wrote:
> Hello,
>
> OK, this is a Ruby question, even though it's for a RoR project :-D
> I am a pretty hopeless Ruby programmer. I am getting better, but when
> it's about eval(), symbols and introspection, I still crumble...
> Besides, I am not even sure what I want to do is actually possible in
> Ruby.
>
> At one point, in my program I have this:
>
>  def create
>     @channel = Channel.new(params[:channel])
>     if @channel.save
>       flash[:notice] = 'Channel was successfully created.'
>       redirect_to :action => 'index'
>     else
>       render :action => 'new'
>     end
>   end
>
> What I want to do, is being able to write:
>
>  def create
>    ApplicationController::standard_create(:channel)
>  end
>
> That "standard_create" function should execute the code, but in
> create()'s scope.
> What I don't know is:
>
> 1) How to keep the code execute by
> ApplicationController::standard_create in create()'s scope
> 2) From ApplicationController::standard_create(name), what to do to
> "create the code"
>
> Any help would be _immensely_ appreciated!
>
> Thanks a lot,
>
> Merc.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


-- 
- Simen