1

I have a multi select box in my file.

<%= c.select(:city, [['Indore', 'Indore'],
                                  ['Delhi', 'Delhi'],
                                  ['Kolkata', 'Kolkata'],
                                  ['Hydrabad', 'Hydrabad'],
                                  ['Jabalput', 'Jabalput']
                                 ],{ },
                                   { :multiple => true }
                                 ) %>

when i select "Hydrabad" and "Jabalpur" cities and submit the form then params is coming like

(rdb:3359) p params
{"utf8"=>"✓", "authenticity_token"=>"SsuXi8K205lHGsKJgkqGpps09gCbehJoO/R8vXGWx+g=", "city"=>{"city"=>["", "Hydrabad", "Jabalput"
]}, "commit"=>"Save City", "controller"=>"posts", "action"=>"addCity"}

So in the city "city"=>{"city"=>["", "Hydrabad", "Jabalput" ]}

, first value is coming blank i.e coming before "Hydrabad"

Please clear this behavior? I don't want that blank value as first position.

sumit
  • 189
  • 3
  • 11
  • Please refer this link: [Answer][1] [1]: http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde It will help you. – Vijay Chouhan Aug 29 '13 at 11:29
  • Please refer this link: [Answer][1] [1]: http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde It will help you. – Vijay Chouhan Aug 29 '13 at 11:31

2 Answers2

0

I think the following link might help you. You want to remove the first element from the list (the unnecessary extra one) when you submit the form.

Ruby: What is the easiest way to remove the first element from an array?

You want to use the shift function, more specifically.

Community
  • 1
  • 1
Mattsjo
  • 627
  • 5
  • 11
0

The issue is resolved, actually i used

city.delete_if{|x| x.empty?

after executing above code the city array is coming

{"city"=>["Hydrabad", "Jabalput" ]}

Please let me know if there is any other better solution?

sumit
  • 189
  • 3
  • 11