Extra field being added to db with multiple select?


#1

Hi all,

First of all, please bear in mind my absolute noobness to Rails and in
fact to programming in general. I have a question regarding a
multiple select box. I’ve managed to get it working with the
following code in my _form view:

Tech
<%= select_tag('vacancy[tech][]', options_for_select(['css', 'php', 'Java']), {:multiple => true}) %>

The problem is, when multiple values are selected, extra dashes get
added to the DB. A direct sql query to the db shows this for this
field:

— - css - php - Java

The dash added as a field separator is fine, but the 3 dashes at the
beginning is not… So I tried to find out where these dashes where
being added, and using the Tamper Data Firefox extension showed me
that the request was being sent nicely, with the same number of
parameters that were selected. The next logical step seemed to be to
check the controller, so I added the following code to the default
create method:

def create
string = params[:vacancy].fetch(“tech”)
@vacancy = Vacancy.new(params[:vacancy])
if @vacancy.save
flash[:notice] = string
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

But the string that gets flashed shows again only the parameters
select, without any field separators or the first three dashes.
What’s going on? What am I missing here? How do I get rid of the
dashes? Again, apologies if I am asking a very stupid question…

Best,

Fernando