Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to create a dynamic form for Contest/Giveaway app

Ryan Swapp asked in General

I've got two models, Contest (has many entries) and Entry (belongs to contest). When I create a contest I want to be able to add some questions that would be dynamically added to the entry form... How would I data model this? Do I also add a Question model that belongs to both Contest and Entry? I'm not sure how to do this..

Reply

Oh boy, the world of dynamic forms. Are you planning on having different types of Questions (like short answer, multiple choice)?

For the simplest thing, if I assume that each Question has the same type of answer (say they are all short answer), you can create an Entry with nested Answer records. Each Answer belongs_to a Question and also belongs_to Entry. You can then generate the form and dynamically add fields_for :answers for each of the questions and save the answers as part of the Entry (and validate them).

It gets more complicated if you want different types of questions though. Might check this out: http://vysakh0.github.io/complex-dynamic-forms-in-rails/

Reply

Haha I definitely understand the "oh boy" sentiment. I've never had to make anything this complex in Rails before (and I doubt it's even relatively complex) and it's totally tripping me up. I don't really know a lot about embedding other model's data into forms so it sounds like I have my work cut out for me. I'm not sure we will be doing different answers to questions. I was thinking that we'd start with simple yes or no type questions and just rock booleans (I will probably need to allow other types of questions in the future though). I think I understand what you are saying though... So I will have 4 models: Contest, Entry, Question, and Answer. When I create the Contest, I will also create Questions for that Contest (Question belongs to Contest) (Ugh.. I guess I'll also have to have a dynamic form when creating a contest). The Entry has many Answers records which belong to a Question. But the Question does not belong to the Entry right? It only belongs to the contest?

Dang, this app seems like it might be a real challenge for me to make. Hopefully I'll be able to figure it out. I'll check out that article and see if I can swing it.

Reply

Don't worry, it's definitely a complex thing to wrangle. Not only do you have to deal with nested forms, but you also have to dynamically generate forms.

You can set a question_type attribute on Question to the string of boolean so that int the future you can use that attribute to determine what format of answer you need to record. As for recording answers, you may want to do something like a generic text field and then parse the result into a boolean so that you can easily add things like short answer questions later without having to update the Answer format.

The 4 models should be all you need and I'd recommend using something like Cocoon for making the Contest which would let you dynamically add more Questions to it. You won't need it for Entry because you'll know the questions and just need to generate one Answer per Question in the Contest to save with the Entry.

Your models should probably look something like this:

Contest

  • has_many :questions
  • has_many :entries

Question

  • belongs_to :contest
  • has_many :answers

Entry

  • belongs_to :contest
  • has_many :answers

Answer

  • belongs_to :entry
  • belongs_to :question
Reply

Now that I'm thinking about it, this would make for an awesome screencast.

Reply

I hadn't heard of Cocoon before, I'll check it out! Thanks for laying out those models for me. I went and watched the railscast on nested and dynamic forms and things are seeming to be a little clearer.. I'll keep working on it.

Yes, I think a screencast on this would be awesome!!

Thanks for your help!

Reply

ActionForm might suits your needs.
As the Rails team is taking over it, it would be great if @excid3 make a screencast about it.

Reply

Oh I did not know that. Thanks, I'll check it out!

Reply

I definitely need to cover ActionForm soon. I'm glad the Rails core team members are actively working on it.

Reply

I created a extendable gem to solve dynamic form problem
https://github.com/rails-engine/form_core

I'm not done doc yet, but It has a full-featured dummy app for evolution that can create forms, fields (including nested fields to support like shopping list), attachment uploading is also support by integrate with ActiveStorage

Reply

I will use your gem Juin Jiang, it's exactly what i need

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.