ruby on rails - Dynamic url_for with varying amount of nesting -
i'm building helper method allows me pass array options available management of object (like edit, delete, etc.). simplified version of method looks this:
def management_links(instance, actions, *parent) actions.each |action| if (can? action, instance) has_options = true case action when :destroy options = {content: glyphicon('trash') + " delete #{instance.class.to_s}", class: "delete #{instance.class.to_s.downcase}", method: :delete} url = url_for [parent, instance] end end end end
as can see works objects nested once (passing 1 parent model) structure:
parent_model/parent_id/model/id/action
but have model nested twice, won't cut anymore. tried passing array [@grandparent, @parent], doesn't work since url_for has array.
is there way allow me passing 'unlimited' parent objects work url_for?
*parent
part of array (if present), why not declare such , push instance it:
def management_links(instance, actions, *parent) parents = array(parent) if parent new_url = parents ? parents << instance : instance actions.each |action| if (can? action, instance) has_options = true case action when :destroy options = {content: glyphicon('trash') + " delete #{instance.class.to_s}", class: "delete #{instance.class.to_s.downcase}", method: :delete} url = url_for new_url end end end end
i use array()
ensure parent
correct data type (you may pass single instance of var).
off topic, in pursuit of convention, should read nesting more 1 layer:
resources should never nested more 1 level deep.
Comments
Post a Comment