views:

78

answers:

0

What's the standard for applying default values to Rails models from links? Here's the situation:

There is a model in Spree called ProductGroup, which is a collections of settings defining how to find a Product (based on a bunch of property values you set). I want to be able to use this structure for defining common ProductGroups:

  • Event Products
  • Subscribable Products
  • Tickets
  • Donations ...

So if I am on a "Create Event" page, and the user clicks "Create Tickets", I want this to take them to the ProductGroups page, so they can create a new ProductGroup for that event, say "My Cool Event Products".

I want to do this as easy as I can without having to override/extend controllers/models. The solution I am thinking about using is to have a :defaults hash passed from a link_to method in a view to the controller, like so:

<%= link_to "Create Event-related Products", new_admin_product_group_url(:defaults => {:name => "Event Name and Date: " + Time.now.to_s}) %>

With that I could then write a generic plugin basically that just applies any properties inside params[:defaults] in the controller to the model.

Is there something like that out there? Or is there a better/recommended way to do this, maybe with defining a custom product_groups_with_event_defaults_url type thing? Or maybe something to do with request.env['HTTP_REFERER'], though that doesn't seem very reliable.

The catch is, I still want pretty urls, so none of this:

http://localhost:3000/admin/commerce/product_groups/new?defaults%5Bname%5D=Event+Name+and+Date%3A+Mon+Jan+25+21%3A03%3A05+-0700+2010