When I'm saving multiple select from a ruby on rails form it appears to be adding a blank element at the front. How do I remove it? The field is selected_player.
{"utf8"=>"✓",
"authenticity_token"=>"H8W7qPBezubyeU0adnTGZ4oJqYErin1QNz5oK0QV6WY=",
"schedule"=>{"event"=>"1",
"result_id"=>"",
"time"=>"26/10/2012",
"duration"=>"15",
"arrival_time"=>"14",
"location_id"=>"25",
"selected_players"=>["", "38", "41"],
"team_id"=>"1",
"opponent_id"=>"7",
"home_or_away"=>"Home"},
"commit"=>"Save Event"}
controller
def update
@schedule = Schedule.find(params[:id])
@user = User.find(current_user)
@players = User.where(:team_id => current_user[:team_id]).all
respond_to do |format|
if @schedule.update_attributes(params[:schedule])
Notifier.event_added(@user,@schedule).deliver
format.html { redirect_to(@schedule,
:notice => "#{event_display_c(@schedule.event)} vs #{@schedule.opponent.name} was successfully updated.") }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @schedule.errors, :status => :unprocessable_entity }
end
end
end
This works for empty strings:
array.delete_if(&:empty?)
To filter out empty strings and nil values use:
array.delete_if(&:blank?)
Example:
>> a = ["A", "B", "", nil]
=> ["A", "B", "", nil]
>> a.delete_if(&:blank?)
=> ["A", "B"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With