开发者

Why my controller params always get single value from multi-select fields?

开发者 https://www.devze.com 2023-02-06 03:44 出处:网络
I have defined a multiple select fields in my form: index.html.haml =form_for :mydata, {:url=>\"/datas/render_datas\", :method=>:post} do |form|

I have defined a multiple select fields in my form:

index.html.haml

=form_for :mydata, {:url=>"/datas/render_datas", :method=>:post} do |form|

  =select_tag('cars',options_from_collection_for_select(@cars, 'id', 'name'), {:multiple=>'multiple', :class=>'cars'})

  = ...
  =form.submit "Get"

when I submit my form, application calls the controller's render_datas method:

class DatasController < ApplicationController
  def render_datas

    selected = params[:cars]

    # I always get the last clicked car item

    puts selected.to_s 

  end
end

From the rails console, I see the params holds the cars hash, but it always hold the last mouse clicked item, n开发者_如何学Cot the multiple selected array, why??? How to get rid of it?


I think your multiple option should be :multiple => true.

0

精彩评论

暂无评论...
验证码 换一张
取 消