2014-10-06 41 views
0

當我檢查我的變量在控制器談到如下:檢查值

{1=>[#<SurveyAnswer id: 1, survey_subject_respondent_id: 1, survey_question_id: 1, answer_value: "2", comment_value: nil, survey_section_id: 1, survey_instrument_id: 1, survey_competency_id: 1, survey_category_id: 1, survey_id: 1, client_id: 1, client_role_id: 1, created_at: "2014-10-06 07:27:19", updated_at: "2014-10-06 07:27:19">, #<SurveyAnswer id: 2, survey_subject_respondent_id: 1, survey_question_id: 2, answer_value: "3", comment_value: nil, survey_section_id: 1, survey_instrument_id: 1, survey_competency_id: 1, survey_category_id: 1, survey_id: 1, client_id: 1, client_role_id: 1, created_at: "2014-10-06 07:27:20", updated_at: "2014-10-06 07:27:20">], 2=>[#<SurveyAnswer id: 3, survey_subject_respondent_id: 1, survey_question_id: 3, answer_value: "3", comment_value: nil, survey_section_id: 2, survey_instrument_id: 1, survey_competency_id: 1, survey_category_id: 1, survey_id: 1, client_id: 1, client_role_id: 1, created_at: "2014-10-06 07:27:20", updated_at: "2014-10-06 07:27:20">, #<SurveyAnswer id: 4, survey_subject_respondent_id: 1, survey_question_id: 4, answer_value: "1", comment_value: nil, survey_section_id: 2, survey_instrument_id: 1, survey_competency_id: 1, survey_category_id: 1, survey_id: 1, client_id: 1, client_role_id: 1, created_at: "2014-10-06 07:27:20", updated_at: "2014-10-06 07:27:20">], 3=>[#<SurveyAnswer id: 5, survey_subject_respondent_id: 1, survey_question_id: 5, answer_value: "4", comment_value: nil, survey_section_id: 3, survey_instrument_id: 1, survey_competency_id: 1, survey_category_id: 1, survey_id: 1, client_id: 1, client_role_id: 1, created_at: "2014-10-06 07:27:20", updated_at: "2014-10-06 07:27:20">, #<SurveyAnswer id: 6, survey_subject_respondent_id: 1, survey_question_id: 6, answer_value: "3", comment_value: nil, survey_section_id: 3, survey_instrument_id: 1, survey_competency_id: 1, survey_category_id: 1, survey_id: 1, client_id: 1, client_role_id: 1, created_at: "2014-10-06 07:27:20", updated_at: "2014-10-06 07:27:20">]} 

從這我要檢查是否有任何survey_id的存在,則在我顯示視圖文本「繼續」,否則我必須將文本顯示爲「開始」。

我增加了一個循環,如下所示:

<% @survey_answers.each do |survey_answer| %> 
    <% if survey_answer.first.present? %> 
    <% survey_answer[1].each do |sa| %> 
     <% if sa.survey_id.present? %> 
     <%= "Continue survey" %> 
     <% else %> 
     <%= "Start survey" %> 
     <% end %> 
    <% end %> 
    <% end %> 
<% end %> 

但有了這個文本是dispalyed多次。我怎樣才能顯示它只有一次

+0

@survey_answers是哈希和你迭代它,這就是爲什麼在@每個鍵survey_answers你看到的是繼續調查或開始調查。 – 2014-10-06 11:28:27

+0

@NikitaSingh是的,我知道。你能建議我怎麼才能得到正確的結果 – Neha 2014-10-06 11:47:01

+1

@neha,我沒有得到你的問題。請你詳細說明它... – Sanket 2014-10-06 11:58:34

回答

0

請儘量就像這樣:

<% if v.values.flatten.map(&:survey_id).compact!.present? %> 
    <%= "Continue survey" %> 
<% else %> 
    <%= "Start survey" %> 
<% end %>