views:

43

answers:

1

I'm working on a basic garden logging application which consists of gardens, plants and planted plants. Each user has one or many gardens, plants are master definitions, and a planted plant can be thought of as an instance of a plant in a specific user's garden.

In my routes.rb file I have the following:

map.resources :gardens do |gardens|
  gardens.resources :planted_plants, :has_many => :plant_log_entries, :collection => { :filter => :post, :choose_garden => :post}
  gardens.resources :garden_log_entries 
end 

map.resources :plants

This makes sense to me when retrieving a list of planted_plants in a user's garden, but I'd like to create a planted_plant record from the index of plant. The problem is, a user can have multiple gardens. How can I create a new form for a planted_plant that allows the user to specify which garden should be used?

The current route requires a garden_id - which makes sense for retrieval, but I'd like to supply that as a parameter for creation.

Thanks in advance for any help!

A: 

I would add another (non-nested) route, to allow access to the PlantedPlantsController#create action without specifying the garden_id in the URL:

map.resources :planted_plants, :only => :create

This will allow you to post your form to /planted_plants. As for the form itself, you'll probably need something like this:

<% form_for @planted_plant do |p| %>
  <%=p.label "garden_id", "Garden" %>
  <%=p.collection_select :garden_id, current_user.gardens, :id, :name %>
  ... other fields ...
<% end %>
Teoulas
That's exactly what I ended up doing - works perfectly! Thanks!
Bobby B