Forum: Ruby on Rails Extra field being added to db with multiple select?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
garrido (Guest)
on 2007-03-29 04:22
(Received via mailing list)
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:

<p><label for="vacancy_tech">Tech</label><br/>
<%= 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
This topic is locked and can not be replied to.