Check Partial Rails Table

I have a form that I am trying to create that is partitioned. Each section saves some information in this table, and then proceeds to the next page. I am trying to find the best way to check the data for each section of the model. I should have used something like validates_presence_of, but it expects that all the data that will be checked will be available when saving ... In my case, the whole table will not be filled until it goes through each section.

What is the best way to do this if there is a way?

+3
source share
2 answers

:if :unless. "step" , , .

class Item < ActiveRecord::Base
  validates_presence_of :name # step 1
  validates_presence_of :category_id, :if => Proc.new { |i| i.step >= 2 } # step 2
  validates_presence_of :description, :if => Proc.new { |i| i.step >= 3 } # step 3
end

.

def update
  @item = Item.find(params[:id])
  @item.step += 1
  if @item.update_attributes(params[:item])
    # ...
  end
end

.

+7

gem grouped_validations, . , .

github.

+2

Source: https://habr.com/ru/post/1714576/


All Articles