2014-10-20 1 views
0

질문 모델 및 답변 모델이 있습니다.Rails4 : Formtastic3.0, 복수 인스턴스 (응답 모델)를 동시에 저장

각 질문마다 사용자 당 하나의 답변을 포함 할 수 있습니다. 나는 모든 질문에 답변을 사용자에게 양식을 미리 설정하려고하지만, 정말 여기 formtastic

함께 할 방법을 알아낼을 couldnt하는 것은 내가 몇 가지를 얻기 위해 노력하고 지금까지

- @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 

이 무엇 그러나 나는 "정의되지 않은 메서드`클래스에 대한 MODEL_NAME ': 클래스"가 계속 How to loop through two alternating resources on a form?에서 힌트 @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

]를 추가 한 후 제출 갖고 싶은 것, 그것은해야

3] 나는 그 만드는 경우 simple_form 보석을 사용하여 마음을 해달라고 텍스트 상자에 미리 poulated 쉽게 생활

+0

귀하의 모델은 어떻게 보이며 서로 어떻게 관련되어 있습니까? 질문, 답변 및 사용자 모델이 있지만 어떤 질문이 양식 (어쩌면 퀴즈 모델)에 나타나는지 정의하는 항목이 있습니까? –

+0

모든 질문이 양식에 나타납니다. @questions = Question.all – codeObserver

답변

0

당신은 당신의 폼에 단일 개체를 전달해야보다는 @questions에 대한 양식을 만드는 도우미 (@questions은 일련의 질문입니다.) 이를 달성하는 좋은 방법은 form object입니다.

# app/forms/questions_form.rb 
class QuestionsForm 
    include ActiveModel::Model 

    def initialize(user) 
    @user = user 
    end 

    def questions 
    @questions ||= Question.all 
    end 

    def submit(params) 
    params.questions.each_pair do |question_id, answer| 
     answer = Answer.find_or_initialize_by(question_id: question_id, user: current_user) 
     answer.content = answer 
     answer.save 
    end 
    end 

    def answer_for(question) 
    answer = answers.select { |answer| answer.question_id == question.id } 
    answer.try(:content) 
    end 

    def answers 
    @answers ||= @user.answers.where(question: questions) 
    end 
end 

그런 다음 컨트롤러에서, 당신은 것입니다 :보기에

# app/controllers/submissions_controller.rb 
Class SubmissionsController < ApplicationController 
    ... 
    def new 
    @questions_form = QuestionsForm.new(current_user) 
    end 

    def create 
    @questions_form = QuestionsForm.new(current_user) 

    @questions_form.submit(params[:questions_form]) 
    redirect_to # where-ever 
    end 
    ... 
end 

당신의 라인을 따라 뭔가 할 것입니다 :

# app/views/submissions/new.html.haml 
= form_for @questions_form, url: submissions_path do |f| 
    - @questions_form.questions.each do |question| 
    %p= question.content 
    = f.text_field "questions[#{question.id}]", value: @questions_form.answer_for(question) 
    = f.submit "Submit" 

이에 formtastic 사용하지 않습니다 모두, 그냥 평범한 레일 형태의 도우미를 사용하고 있습니다.

이 코드는 테스트되지 않았으므로 작동 여부는 확실하지만 잘하면 올바른 방향으로 인도 할 수 있습니다.