user3189916
user3189916

Reputation: 768

Handling multiple params in Ruby

I am having a scenario where I have a form with adding new employees and deleting the saved employees in the same form. While adding it is fine but when we delete one record and click on save, it is throwing validation error in another page. I am trying to block in the same page as below is my controller method:

def check_params
  #raise
  return if params[:employee].present? 
  flash[:alert] = 'Please fill in form before submitting.'
  redirect_to edit_employee_url
end

I tried adding

return if params[:employee].present? && params[:employee][:emp_attributes]["0"]["_destroy"] != "1"

But works only for one employee record, if we had more than one record , it will not.

Below are the params captured through raise.

params => "✓", "_method"=>"patch", "authenticity_token"=>”AAABBBVC==", "employee"=>{"emp_attributes"=>{"0"=>{"_destroy"=>"1", "name"=>"test", "start_day_of_the_week"=>"Monday", "sw_id"=>"2"}, "1"=>{"name"=>"test2", "start_day_of_the_week"=>"Monday", "sw_id"=>"3"}}}, "form_step"=>"2", "commit"=>"Save & Continue", "view_context"=>:empdet, "controller"=>"employees", "action"=>"update", "id"=>"29955"} permitted: false>

params[:employee] => {"0"=>{"_destroy"=>"1", "name"=>"test", "start_day_of_the_week"=>"Monday", “sw_id"=>"2"}, "1"=>{"name"=>"test2", "start_day_of_the_week"=>"Monday", “sw_id"=>"3"}}} permitted: false>

Upvotes: 0

Views: 47

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 5995

Try this:

return if params[:employee].present? &&
params[:employee][:emp_attributes].values.any?{|m| m.key?("_destroy")}

Upvotes: 0

Related Questions