Ruby on Rails 条件渲染不起作用

问题描述 投票:0回答:1

这是我的考勤控制器中

mark_attendance
方法的一部分。该方法在扫描二维码后调用,如果 my_student_course.size > 1,则应显示模式。 `如果讲师 my_student_course =讲师.lecturer_units.flat_map(&:students_courses).uniq.select { |course| course.student_id == current_student.id } 如果 my_student_course.size > 1 @show_modal = true @students_attendance_courses = my_student_cours

      # Render a pop-up page with a list of results and radio buttons for selection
    else
      # If there is only one result or none, proceed with the first result
      my_student_course_id = my_student_course.first&.id

      students_course = StudentsCourse.find_by(id: my_student_course_id)

      if students_course
        attendance = Attendance.find_or_create_by(
          students_course: students_course,
          attendance_date: Date.today,
        ) do |attendance|
          attendance.present = true
        end

        if attendance.persisted?
          puts "Attendance marked successfully"
          render json: { message: "Attendance marked successfully" }
        else
          render json: { error: "Error saving attendance" }, status: :unprocessable_entity
        end
      else
        render json: { error: "StudentsCourse not found for the given students_course_id" }, status: :unprocessable_entity
      end
    end
  else
    render json: { error: "Lecturer not found for the given lecturer_id" }, status: :unprocessable_entity
  end`

这是我的

scan.html.erb
进行扫描的地方。

<%= render 'modal' if @show_modal %>
<div id="scanQrCodeModal">
  <h1>Scan QR Codes</h1>
  <div class="section">
    <div id="my-qr-reader"></div>
  </div>
</div>

这是考勤视图目录中的

_modal.html.erb
部分。

<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
    <script>
    document.addEventListener('DOMContentLoaded', function() {
      var myModal = new bootstrap.Modal(document.getElementById('exampleModal'));
      myModal.show();
    });
    </script>

当满足此条件时,模式将不起作用

if my_student_course.size > 1
。请指教。这是完整项目的 github 存储库:https://github.com/SimonGideon/Online-Attendance/tree/mark-attendance

ruby-on-rails ruby erb ruby-on-rails-7
1个回答
0
投票

尝试应用下面的解决方案之一

Soution-1

<% if @show_modal %>
  <%= render 'modal' %>
<% end %>

<div id="scanQrCodeModal">
  <h1>Scan QR Codes</h1>
  <div class="section">
    <div id="my-qr-reader"></div>
  </div>
</div>

<%= javascript_include_tag 'https://unpkg.com/html5-qrcode' %>
<%= javascript_include_tag 'html5-qrcode.min' %>
<%= javascript_include_tag 'qr-scanner' %>

解决方案-2

<%= render 'modal' if defined?(@show_modal) && @show_modal %>
<div id="scanQrCodeModal">
  <h1>Scan QR Codes</h1>
  <div class="section">
    <div id="my-qr-reader"></div>
  </div>
</div>

<%= javascript_include_tag 'https://unpkg.com/html5-qrcode' %>
<%= javascript_include_tag 'html5-qrcode.min' %>
<%= javascript_include_tag 'qr-scanner' %>
© www.soinside.com 2019 - 2024. All rights reserved.