我有問題模型和答案模型。Rails4:Formtastic3.0,同時保存多個實例(對於Answer模型)
每個問題每個用戶可以有一個答案。我試圖預設給用戶的形式來回答所有的問題,但想不出真正弄清楚如何與formtastic
這裏做的是我迄今爲止
- @questions.each do |q|
= q.content
- ans = @user.answers.where(question_id:q.id).first.try(:content) || q.description
= semantic_form_for @answers do |f|
= f.label q.content
= f.inputs :content, placeholder: ans
= f.actions
我試圖得到一些從How to loop through two alternating resources on a form?但我不斷收到「類未定義的方法`MODEL_NAME」:類」暗示了@questions如果我嘗試:
= semantic_form_for @questions do |q|
= q.input :content
= q.semantic_fields_for @answer do |a|
= a.inputs :content
= q.actions
基於Railscast 198, but using formtastic這是我嘗試不工作,要麼:
- semantic_form_for :Answer, :url => api_v1_answers_path, :method => :put do |f|
- @questions.each do |q|
- f.fields_for 'questions[]', q do |ff|
= q.content
= ff.input
= submit_tag "Submit"
注:
1]我想有用戶按提交一次後,他又增加/編輯所有的答案
2]如果有一個答案已經存在,它應該是預poulated在文本框中
3]我用simple_form寶石如果讓不介意生活更輕鬆
你的模型是什麼樣子的?它們是如何相互關聯的?你有一個問題,答案和用戶模型,但是你有什麼定義了什麼問題出現在表單中(也許是一個測驗模型)? – 2014-10-20 23:17:00
所有問題都出現在表單中。 @questions = Question.all – codeObserver 2014-10-20 23:19:09