views:

314

answers:

0

Hi,

In my code, I have 4 models: Plan, Choice, Venue, CustomSuggestion, in order to allow users to create "plans" which consist of choices, which in turn consists of a suggested.

My code looks something like this:

class Plan < ActiveRecord : Base
  has_many :choices
end
class Choice < ActiveRecord : Base
  belongs_to :plan
  belongs_to :suggestion, :polymorphic => :true
end
class Venue < ActiveRecord : Base
  has_many :choices, :as => :suggestion
end
class CustomSuggestion < ActiveRecord : Base
  has_many :choices, :as => :suggestion
end

The problem I am having is figuring out a way to create a nested form to create and edit a plan, such that the user can choose for each choice what type of suggestion it should be. Before, a choice didn't have a polymorphic suggestion, but had to have a venue. Now, I am trying to provide more flexibility and allow a user to choose to suggest a suggestion not limited by picking a specific venue.

Here is some of the code that used to work:

<% f.fields_for :choices do |choice_form| %>
  <%= choice_form.collection_select :venue_id, all_venues, 
                :id, :name, {:prompt => 'Pick a place...'} %>
  <%= choice_form.hidden_field :rank %>
  <%= choice_form.hidden_field :nested 
<% end %>

For my new code, I want to provide the collection_select of venues only when the user chooses "venue" as the type, otherwise I want to show a text box where the user can input his/her own text.

I am not sure how to get the actual suggestion_type of each individual choice because I don't know how to get the choice object from a choice_form. I tried putting a <% @plan.choices.each |choice| do %> block around the fields_for, then having <% f.fields_for choice ...%>, but then the forms generated would be individual, and they'd all have name plan_choice_suggestion or something like that (rather than plan_choice_0_suggestion, etc).

Anybody know of a good way to do this?

Thanks, Eric