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 %>

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

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

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

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

<%= link_to 'Back', todos_path %>
6 Years
Discussion Span
Last Post by Taywin

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.