R
R
rundom12017-02-23 12:34:06
Ruby on Rails
rundom1, 2017-02-23 12:34:06

How to use simple_form in rails?

Good afternoon. How to make users in rails be able to generate forms to fill out themselves.
If there is a solution without this gem, then it is also suitable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zaporozhchenko Oleg, 2017-02-23
@rundom1

simple_form is just a helper, i.e. a function that generates HTML. It doesn't know any logic.
The task that you are trying to solve is very non-trivial, but it is solved approximately as follows
1) Form the rules for forming the form, what type of field can be added to it, what values ​​are available, validation. All these rules are most conveniently stored in a relational structure
2) When you need to display a form, you need to build a new class based on ActiveModel::Model this class will have all the features of a Rails model but will not be tied to a specific table with its fields. This class hangs validations and other logic on itself, based on point 1
3) If the database is PostgreSQL, then the easiest way is to save all values ​​​​in one jsonb field
In the general case, the task that you described is quite complicated, I won’t show a code example because there are a lot of them, it will rather confuse you. Try to think first, maybe you can change the task, not allow adding absolutely any fields, but simply choose from several sets of fields. Then STI works well with the same jsonb field and access via store

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question