J
J
justed_ss2016-03-30 13:04:03
JavaScript
justed_ss, 2016-03-30 13:04:03

Best way to generate errors for Rails, nested_attributes in json?

There is a model that takes attributes for a nested model, the result of the form is returned as json, and then it is drawn

class Order < ActiveRecord::Base
  accepts_nested_attributes_for :entries
end

class Entry < ActiveRecord::Base
  accepts_nested_attributes_for :members, allow_destroy: true
end

And here we submit the Order form, which has fresh entries, which has fresh members, with validation errors
Fields in the form in the following form, in the tradition of Rails
<input autocomplete="off" class="string required form-control" id="order_entries_attributes_1459329286687_members_attributes_1459329286739_title" name="order[entries_attributes][1459329286687][members_attributes][1459329286739][title]" placeholder="Наименование" type="text">

Controller
class OrdersController
  def update
    if @order.update(order_params)
      render json: @order
    else
      render json: @order.errors, status: :unprocessable_entity
    end
  end
end

And that's what's coming back
{"entries.members.client_id":["введите значение"],"entries.members.clinic_id":["введите значение"]}

The problem is that based on this answer, it is impossible to determine exactly which of the entries, and with which of the nested members, the validation error is located. Maybe there is a gem or some other approach?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question