I have a model called task.rb as follows:
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# inputpath :string(255)
# outputpath :string(255)
# inputsize :integer
# outputsize :integer
# operator :string(255)
# operationtype :string(255)
# created_at :datetime
# updated_at :datetime
#
class Task < ActiveRecord::Base
@@tasktypes = ["csv", "db"]
def tasktypes
@@tasktypes
end
end
What Im trying to do in the view rendered for the "new" action, I want to give the user an option as checkboxes to select one or many values from @@tasktypes that will be the value for attribute: "operationtype"
This is my form:
<%= form_for(@task) do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :inputpath %><br>
<%= f.text_field :inputpath %>
</div>
<div class="field">
<%= f.label :outputpath %><br>
<%= f.text_field :outputpath %>
</div>
<div class="field">
<%= f.label :inputsize %><br>
<%= f.number_field :inputsize %>
</div>
<div class="field">
<%= f.label :outputsize %><br>
<%= f.number_field :outputsize %>
</div>
<div class="field">
<%= f.label :operator %><br>
<%= f.text_field :operator %>
</div>
<div class="field">
<%= f.label :operationtype , "What would you like to do with the file?"%><br>
<%= f.collection_check_boxes(:operationtype,?? ,@@tasktypes, ?? %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
UPDATE:
I just made a slight change to the model attribute "operationtype" from string to text and also serialized it in the model see below:
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# inputpath :string(255)
# outputpath :string(255)
# inputsize :integer
# outputsize :integer
# operator :string(255)
# operationtype :text
# created_at :datetime
# updated_at :datetime
#
class Task < ActiveRecord::Base
serialize :operationtype, Array
@@tasktypes = ["csv", "db"]
#validates :inputpath, :outputpath, presence => {:message => "Must provide an inputpath and outputpath for file processing" }
def self.tasktypes
@@tasktypes
end
end
As you can see above Im trying to figure out how to get the checkboxes to work. I would be grateful if someone can assist with this. Ive looked through the docs and there doesn't seem to be a way to make this work, but maybe there is a hack?
You could use the check_box_tag
helper, kinda like that
<% tasktypes.each do |type| -%>
<label><%= type %></label>
<%= check_box_tag 'task[operationtype][]', type %>
<% end -%>
Although I'd have it as an array property on the model and then use the select
helper with the multiple
option switched on. Would be easier to track and handle the state of the picked values