I
I
IvanN7772016-10-03 10:27:14
Ruby on Rails
IvanN777, 2016-10-03 10:27:14

It was necessary to use 2 models in the form (what is better to use reform or nested_atributes) what does it depend on?

reform to use when you need additional form validation logic?
It is also not clear that specific attributes are usually indicated in the manual for it.

class AlbumForm < Reform::Form
  include Composition

  property :id,    on: :album
  property :title, on: :album
  property :songs, on: :cd
  property :cd_id, on: :cd, from: :id
end

But what if I need to specify all the attributes from one model? and there are 20
of them. I also worked with Virtus in essence the same thing, or I'm mistaken.
Perhaps the only plus, I do not have to set types for attributes, for reform, which is generally useful.
Or I don't understand something.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2016-10-03
@IvanN777

If the logic is simple and no extensions are expected, then use nested_attributes, because they are already there.
If the logic is complex, then the next question is - should such a complex piece be 1-2 in the system? If so, create an app/services folder (app/interactors) and put the AlbumSaver class there. And the app/form_objects folder with the AlbumUpdateForm class. And call AlbumSaveForm in AlbumSaver.
If you have a lot of places where you need very complex forms, then add some form management gem to the previous approach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question