Search code examples
ruby-on-railsrubyforms

form_tag not triggering patch call in rails


Below is the code to update the statuses for different students but the issue is after I am made some changes to the statues clicking update status does nothing.

students/manage_users.html.erb

<div class="col-sm-9 col-xs-12">
  <div class="content" role="main" id="main-content">
    <article>
      <div>
        <div class="table-responsive">
          <table class="table table-bordered">
            <thead>
              <tr>
                <th><%= link_to "Primary Student", :sort => "student_1_firstname" %></th>
                <th>Admin Status</th>
                <th>Superadmin Status</th>
              </tr>
            </thead>
            <%= form_tag update_status_path, :method => 'patch' do %>
              <tbody>
                <% @students.each do |student| %>
                  <tr> 
                    <td><%= link_to student.email,student_path(student) %></td>

                    <td><%= check_box_tag "students[#{student.id}][admin]", true, student.admin, class: 'status', data: {id: student.id, type: 'admin'} %></td>
                    <td><%= check_box_tag "students[#{student.id}][superadmin]", true, student.superadmin, class: 'status', data: {id: student.id, type: 'superadmin'} %></td>
                  </tr>
                <% end %>
              </tbody>
            </table>
            <%= submit_tag "Update Status" %>
          <% end %>
        </div>
        <div class="clearfix"></div>
      </div>
    </article>
  </div>
<!-- .content -->
</div>
<!-- END .main_column_css -->

routes.rb

patch 'main-admin/manage_users', to: 'students#update_status', as: 'update_status'

Controller

class StudentsController < ApplicationController
  def update_status
    @students = Student.all
    students = params[:students]
    @students.each do |student|
      student.update_attributes(admin: students[student.id.to_s] && students[student.id.to_s]['admin'] ? true : false, superadmin: students[student.id.to_s] && students[student.id.to_s]['superadmin'] ? true : false)
    end
  end
end

Please help me fix this


Solution

  • Your HTML is invalid.

    According to MDN the permitted contents of a <table> element is restricted to a very narrow set of flow content:

    In this order:
    
     1. an optional <caption> element, 
     2. zero or more <colgroup> elements, 
     3. an optional <thead> element, 
     4. either one of the following: 
        - zero or more <tbody> elements 
        - one or more <tr> elements 
     5. an optional <tfoot>
        element
    

    Forms are not flow content.

    You also have mismatched closing tags which just creates a jumble. Move the form tag outside of the table.

    <div class="col-sm-9 col-xs-12">
      <div class="content" role="main" id="main-content">
        <article>
          <div>
            <div class="table-responsive">
              <%= form_tag update_status_path, method: :patch do %>
                <table class="table table-bordered">
                  <thead>
                    <tr>
                      <th><%= link_to "Primary Student", :sort => "student_1_firstname" %></th>
                      <th>Admin Status</th>
                      <th>Superadmin Status</th>
                    </tr>
                  </thead>
                  <tbody>
                    <% @students.each do |student| %>
                    <tr> 
                      <td><%= link_to student.email, student_path(student) %></td>
                      <td><%= check_box_tag "students[#{student.id}][admin]", true, student.admin, class: 'status', data: {id: student.id, type: 'admin'} %></td>
                      <td><%= check_box_tag "students[#{student.id}][superadmin]", true, student.superadmin, class: 'status', data: {id: student.id, type: 'superadmin'} %></td>
                    </tr>
                    <% end %>
                  </tbody>
                </table>
                <%= submit_tag "Update Status" %>
              <% end %>
            </div>
            <div class="clearfix"></div>
          </div>
        </article>
      </div>
    </div>