1

I have a page where I may add n number of questions. And my questions partial contains a form as follows:

= form_for :question, :url => questions_path do |f|
  = f.text_field :title

What I get in params is "question"=>{"title"=>"Some Name"}. But, if I click Add New Question button the same partial is rendered below. My problem is I still get the first questions params even if I have 2 forms now. Is there a way to get params like

"question"=>{["title"=>"Some Name"], ["title"=>"Some Other Name"]}
6
  • So you get n HTML forms in one page, right? I would have a collection of questions in the model. At every Add New Question I would add a new empty Question to the model and let the page render the list of questions in a single HTML form. Commented Jan 14, 2015 at 13:54
  • Yes, I'm willing to consolidate the params of all forms into one array so that I can iterate over them. Commented Jan 14, 2015 at 13:56
  • @Alex Its like I don't have questions predefined but are dynamically given by user. So I need to have array of questions and iterate over them to create records for each. Commented Jan 14, 2015 at 13:59
  • As far as I know, you can't receive all the inputs values on the server side if they are in several HTML forms in a single HTML page. Le's say you have two <form /> each having an <input />. A submit button can submit to the server a specific form, not both. Thus only the input value from that form is sent to the server (unless you have a javascript that read all data and... but this is not what you want). Commented Jan 14, 2015 at 14:01
  • "Its like I don't have questions predefined but are dynamically given by user." - that's fine. Still I would go with a model having a collection of questions. The very first time I would add a new empty Question to the model. The page will display it with empty inputs. Now Add New button adds a new empty Question to the model, and the page will show two questions, and so on. (Haven't tried this, but should work.) Commented Jan 14, 2015 at 14:04

1 Answer 1

4

try this

= form_for :question, :url => questions_path do |f|
  = f.text_field :title, name: "question[title][]"

for more info refer http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.