Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 检查hashruby中的值_Ruby On Rails 4_Hash - Fatal编程技术网

Ruby on rails 4 检查hashruby中的值

Ruby on rails 4 检查hashruby中的值,ruby-on-rails-4,hash,Ruby On Rails 4,Hash,当我在控制器中检查变量时,结果如下: {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_i

当我在控制器中检查变量时,结果如下:

{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_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 %>
由此,我必须检查是否存在任何调查id,然后在视图中我必须显示文本Continue,否则我必须将文本显示为Start

我添加了一个循环,如下所示:

{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_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 %>

但是有了这个,文本会被显示多次。如何仅显示一次请按如下方式尝试:

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

@survey_answers是散列,您正在对其进行迭代,这就是为什么@survey_answers中的每个键都会看到“继续调查”或“开始调查”。@NikitaSingh是的,我知道。你能告诉我怎样才能买到合适的吗result@neha,我不明白你的问题。请详细说明一下…@Sanket从上面的散列中我需要检查是否存在任何调查id,然后显示“继续”文本,否则显示“开始”,因此您要检查第一个调查答案是否具有调查id,并且不想检查该键中数组的其余部分?