opticon
opticon

Reputation: 3584

Dynamic forms in rails

So I'm creating a simple application. I have a 'Games' database table and a 'Rules' database table. Each game can have many rules. I want to present a form like this:

Game Title:
Game Description:
  Rule #1: 
  (Click to add another rule)

So the user can click a button to add another text field for additional rules. What is the best way of going about this? Are there form helpers? Any way to return an array of rules? I'm somewhat new to Rails, and my googling didn't provide much help. I may just be being ignorant, but any help would be greatly appreciated!

Upvotes: 5

Views: 14127

Answers (4)

kalibbala
kalibbala

Reputation: 508

What you are looking for are called "dynamic forms" and the answer to your question is in the following Railscasts episode.

  1. http://railscasts.com/episodes/403-dynamic-forms
  2. https://github.com/railscasts/403-dynamic-forms

Its kinda late but i hope it helps someone else.

Upvotes: 3

nowk
nowk

Reputation: 33161

This is old but should be identical to what you are trying to do at the core.

http://railscasts.com/episodes/75-complex-forms-part-3

Just think of Project and Tasks to your Game and Rules.

Upvotes: 2

miked
miked

Reputation: 4499

You'll likely want to use a "game" form with nested "rules" forms. This would be a lengthy answer if I put all the code needed but visiting these should help you right along:

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

Upvotes: 0

Ben Zhang
Ben Zhang

Reputation: 1281

Use javascript. This would help http://railscasts.com/episodes/197-nested-model-form-part-2

Upvotes: 0

Related Questions