How to correctly define the name of a control in a Rails nested form?
I'm using plataformatec's simple_form to generate a nested_attributes form
in a Rails 3.2 app. For one of the form inputs, I need to use a Rails form
helper, but wrap it with simple_form's layout helpers. How do I correctly
set the name for this input manually.
I have Project and Task models. The Task model has a bitmask to set a task
type
class Project
has_many :tasks
end
class Task
belongs_to :project
TASKTYPE = %w[urgent scheduled delegated]
def tasktypes=(tasktypes)
self.tasktype_mask = (tasktypes & TASKTYPES).map { |f|
2**TASKTYPES.index(f) }.sum
end
def tasktypes
TASKTYPES.reject do |f|
((tasktype_mask || 0) & 2**TASKTYPES.index(f)).zero?
end
end
end
In the form
<%= simple_form_for @project do |f| %>
<%= f.simple_fields_for :tasks do |builder| %>
<%= builder.input :fingerprint_mask, label: "Fingerprint" do %>
<% for tasktype in Task::TASKTYPES %>
<%= check_box_tag <<<<check_box_name>>>>, tasktype,
builder.tasktypes.include?(tasktype) %>
<%=h tasktype.humanize %><br />
<% end %>
<%= hidden_field_tag <<<<hidden_field_name>>>>>, "" %>
<% end %>
<% end %>
<% end %>
How do I correctly set the check_box_name and hidden_field_name to ensure
they play nicely with the correct nested record?
No comments:
Post a Comment