Below is the ERB for a new 'To-do' view. Duedate is another table that holds date objects, and there is an association between duedates and todos with a 'duedate_id' in the todo table. Right now there is just a dropdown for any externally created duedates. While creating a new todo, I would like the option to EITHER select a previously created duedate from the drop-down menu or create a new duedate on the spot, and then be able to select it from the dropdown list. Is this possible?

<h1>New todo</h1>

<% form_for(@todo) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :todo %><br />
    <%= f.text_field :todo %>
  </p>

  <p>
    <%= f.label :priority %><br />
    <%= f.collection_select :priority_id, Priority.find(:all), :id, :priority%>
  </p>

  <p>
    <%= f.label :duedate %><br />
    <%= f.collection_select :duedate_id, Duedate.find(:all), :id, :duedate %>
  </p>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', todos_path %>

Yes, but you need to pass in a param of previously selected duedate_id to your newly created @todo in controller.

# example in controller
def new
  @todo = Todo.new
  @todo.duedate_id = params[:duedate_id].to_i>0 ? params[:duedate_id].to_i : nil
  # if duedate_id has a default value, give the default value to the duedate_id instead
end
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.