Answer the question
In order to leave comments, you need to log in
How to make dynamic form generation interact with different models?
Hello! Please suggest / direct me in the right direction how to make such a form.
There are two Question and Answer models.
Accordingly, questions are stored in questions, and possible answers are stored in answer.
I made a form for editing Question:
<%= simple_form_for @question do |f| %>
<%= f.input :question, :label => 'Вопрос' %>
<%= f.input :score, :label => 'Количество баллов' %>
# а тут должны проявлять инпуты для ответов(ответов может быть все время разное количество) они хранятся в отдельной таблице, они связаны по id
<% end %>
Answer the question
In order to leave comments, you need to log in
api.rubyonrails.org/classes/ActiveRecord/NestedAtt...
api.rubyonrails.org/classes/ActionView/Helpers/For...
https://github.com/nathanvda/cocoon
But this is a rather hemorrhoidal topic, you will have to suffer. I advise you to look for tutorials on accepts_nested_attributes, fields_for. Also, this video can help: https://youtu.be/QhdzE1yNs-0?t=1m20s
First, you have questions and answers, for example questions has_many answers,
then you are available with static mode (one field for an answer in your terms)
<%= f.fields_for :answers, @question.answers.new do |answer_fields| %>
<%= answer_fields.text_area :text %>
<%- @question.answers.each do |answer| %>
<%= f.fields_for :answers, answer do |answer_fields| %>
<%= answer_fields.text_area :text %>
#etc
The correct approach for creating forms that involve multiple models is to use Form Objects . I suggest you google about this topic, and also pay attention to these two gems (read the disclaimers):
https://github.com/apotonick/reform
https://github.com/railsgsoc/actionform
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question