10

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

7 Answers 7

20

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"]
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly and can easily be added to the model logic in a before save function.
7

Ref reject! of Array class

params["schedule"]["selected_players"] = ["", "38", "41"]
params["schedule"]["selected_players"].reject!{|a| a==""} #gives params["selected_players"] = ["38", "41"]

3 Comments

thats given me undefined method reject!' for nil:NilClass`
Please check my edited answer change params["selected_players"] to params["schedule"]["selected_players"]
to avoid issues where the params may be incomplete you could add a try : params["schedule"]["selected_players"].try(:delete_if, &:blank?)
2

This should work as well.

params["schedule"]["selected_players"].reject!(&:blank?)

Comments

0

Something like:

params["selected_players"].select!{|val| !val.empty?}

should work

Comments

0

What is "selected_players"? Is it something like "collection_singular_ids" of the collection associations? If so, you can leave it as it is, because ActiveRecord will remove the blank elements from the array with following code:

ids = Array.wrap(ids).reject { |id| id.blank? }

Comments

0

If you want to handle this in the model rather than the controller you can add a setter method like this

def selected_players=(param_array)
  write_attribute(:selected_players, param_array.reject(&:blank?))
end

Comments

-3

I think params["selected_players"].compact is the most succinct.

Docs are here: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-compact

1 Comment

This will not work for empty strings like the OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.