2014-02-14 89 views
0

我有一個表單,它有問題和答案,它包含:student_id,survey_id,question_id,answer_id。我希望每個表格都應該保存與問題等同的記錄。 我有兩個問題:無法正確提交表單值

1-只有第一個屬性(student_id)在保存0價值創造我的行動,以及其他屬性保存爲Null

2-表單提交2個額外的記錄。我目前的形式有3個問題,當我回答3個問題我得到了數據庫內的5個記錄(和第一隻記錄有除student_id = 0空值和其他記錄的所有屬性空值

控制器:

def create 
#student_id saved as 0 and the rest attributes saved as Null 
#always save two extra records with null value even when i answer all questions 
    params[:subject_survey].each do |student, survey, question, answer| 
     @sv_sub = SubjectSurvey.create(student_id: student["student_id"], 
    survey_id: survey["survey_id"], 
    question_id: question, 
    answer_id: answer) 
    end 

    if @sv_sub.save 
     redirect_to surveys_path, notice: "Success." 
    else 
     render :new, notice: "Failed." 
    end 
end 

服務器日誌:

#when answering three questions: 

Started POST "/subject_surveys" for 127.0.0.1 at 2014-02-14 21:54:09 +0200 
Processing by SubjectSurveysController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"C8w1p+tQCTY25SJU5TGJcqCqRoUw1TvVBjEJgpjPMPc=", "subject_survey"=>{"student_id"=>"1", "survey_id"=>"1", "2"=>{"answer_id"=>"7"}, "3"=>{"answer_id"=>"14"}, "4"=>{"answer_id"=>"19"}}} 

#when answering one question: 

Started POST "/subject_surveys" for 127.0.0.1 at 2014-02-14 21:50:29 +0200 
Processing by SubjectSurveysController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"C8w1p+tQCTY25SJU5TGJcqCqRoUw1TvVBjEJgpjPMPc=", "subject_survey"=>{"student_id"=>"1", "survey_id"=>"1", "2"=>{"answer_id"=>"7"}}} 

new.html.erb

<%= form_tag('/subject_surveys', method: :post, class: 'form-horizontal') do %>  
    <% q = 0 %> 
    <% Survey.find(1).questions.order("questions.id asc").each do |question| %> 
     <% q += 1%> 
    <%= hidden_field_tag 'subject_survey[student_id]', "#{current_user.student.id}" %> 
    <%= hidden_field_tag 'subject_survey[survey_id]', '1' %> 
    <h6>Q<%= q %>- <%= question.content %></h6> 
    <div class="clearfix"></div><div class="clearfix"></div> 
    <% if question.question_type == "#{Question::CHECK}" %> 
     <% question.answers.each do |answer| %> 
      <%= check_box_tag "subject_survey[#{question.id}][answer_id][]", "#{answer.id}" %> 
      <%= answer.content %> 
      <div class="clearfix"></div> 
     <% end %> 
    <% elsif question.question_type == "#{Question::RADIO}" %> 
     <% question.answers.each do |answer| %> 
      <%= radio_button_tag "subject_survey[#{question.id}][answer_id]", "#{answer.id}" %> 
      <%= answer.content %> 
     <div class="clearfix"></div> 
     <% end %> 
     <% end %> 
    <% if question.id == Survey.find(1).questions.order("questions.id asc").last.id %> 
     <br> 
    <% else %> 
     <hr style="border-top: 1px solid #000000;"> 
    <% end %> 
    <% end %> 
    <div class="form-actions no-margin"> 
     <%= submit_tag "Submit", class: "btn btn-info pull-right" %> 
    <div class="clearfix"></div> 
    </div> 
<% end %> 

回答

0

我首先看到的一件事是你的控制器中不需要survey["survey_id"]student["student_id"]。您已經映射了它們,並且您只需分別使用surveystudent

另一方面,每個question有一個答案哈希分配給它。所以,你應該這樣對待。

這意味着,在您的初始映射

params[:subject_survey].each do |student, survey, question, answer| 

最後一個參數(回答)沒有立足之地。總之,這不是最合適的,所以請看下面。

此外,第三個參數問題有點模糊......它不是作爲「問題」傳遞,而是作爲指向值的數字傳遞。你應該讓它成爲一個數組。

在您的形式,改變這種:

<%= check_box_tag "subject_survey[#{question.id}][answer_id][]", "#{answer.id}" %> 

到這個(沒有必要告訴表單,該值被標記「answer_id」,你已經知道這一點,所以我們擺脫了[answer_id]部分)

<%= check_box_tag "subject_survey[questions][#{question.id}]", "#{answer.id}" %> 

然後,在你的控制器動作

def create 
    params[:subject_survey][:questions].each do |question, answer| 
     @sv_sub = SubjectSurvey.create(
      student_id: params[:subject_survey][:student_id], 
      survey_id: params[:subject_survey][:survey_id], 
      question_id: question, 
      answer_id: answer) 
    end 
    .... 
+0

感謝答覆,我得到這個'不確定的實現方法具d'each'for nil:NilClass' for questions.each –

+0

thats the params參數:{「utf8」=>「✓」,「authenticity_token」=>「J5ZcUyKbnE7r3gxdeMiP7b7FVZ10Kh7BhUwaq5Mddew =」,「subject_survey」=> {「student_id」= >「1」,「survey_id」=>「1」,「questions」=> {「2」=> {「answer_id」=>「7」},「3」=> {「answer_id」=>「10」 }}},「commit」=>「Submit」}' –

+0

請注意,在表單中,[answer_id]已被刪除...不太確定是否也應該刪除[] –