简体   繁体   中英

Processing Dynamically generated rails form

I am trying to process the following input.

Parameters: {"utf8"=>"✓", "authenticity_token"=>"ogwCJx5J03kvFrve1B/BQ5jBlqsGaqP2En7sAe5XcBA=", "contact_attempt_id"=>"79","3"=>"8", "7"=>"22"}

I can get the contact_attempt_id and response_code easily. the final two parameters are proving to be elusive.

the key is the database id for a question, while the value is the database id for the answer. I have attempted to loop through the questions looking for the id in the params hash as shown below.

questions.each do |q|
  if !params[q.id].nil?
   c = contact_responses.create
   c.question_id = q.id
   c.answer_id = params[q.id]
   c.save
  end

however I am not able to get the answer id.

Your id from database will be an integer. params hash keys are strings/symbols ( HashWithIndifferentAccess )

Change your code to this

questions.each do |q|
  unless params[q.id.to_s].blank?
   c = contact_responses.create
   c.question_id = q.id
   c.answer_id = params[q.id.to_s].to_i
   c.save
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM