Thanks Roy, that could be the cunning magic I'm looking for.

I like the idea of your find_resources method. As you say, it keeps
the routes.rb as the single point for nesting knowledge.

Many thanks,
George


On Oct 27, 6:40 pm, Roy Wright <[email protected]> wrote:
> On Oct 26, 2009, at 6:50 AM, George Adamson wrote:
>
> > Is this a job for a helper method? Named routes? Other?
>
> Here's the helpers that I use (in app/helpers/global_helpers.rb):
>
> module Merb
>    module GlobalHelpers
>      # parent is an Array of model instances or nil
>      def rest_resource(parent, *args)
>        rsrc = nil
>        if parent.nil?
>          rsrc = resource(*args)
>        else
>          rsrc = resource(*([parent].flatten + (args || [])))
>        end
>        rsrc
>      end
>    end
> end
>
> Then to use it in views:
>
>    #actions
>      %ul
>        %li= show_btn(rest_resource(@parent, @resource), 'Show')  
> unless(@parent.nil? && @resource.nil?)
>        %li= new_btn rest_resource(@parent, :appointments, :new), 'New  
> Appointment'
>
> and in controllers:
>
>    def destroy
>      debug 'destroy'
>      get_parent
>      if get_resource.destroy
>        redirect rest_resource(@parent, controller_name.to_sym)
>      else
>        raise InternalServerError
>      end
>    end
>
>    def get_parent
>      if @parent.nil?
>        params.each do |k, v|
>          if k.to_s =~ /(.+)_id/
>           �...@parent ||= []
>           �...@parent << find_resource($1.camel_case, v)
>          end
>        end
>      end
>     �...@parent
>    end
>
>    def find_resource(class_name, id)
>      resource = Kernel.const_get(class_name).get!(id)
>      instance_variable_set(:"@#{class_name.snake_case}", resource)
>    end
>
> One big advantage with this approach is now routes.rb is the single  
> point for nesting knowledge.
>
> HTH,
> Roy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to