0

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 %>
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Taywin
0

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