lundi 22 novembre 2021

Ruby on Rails allow multiple clicks for link_to

I'm new to Ruby on Rails and have found myself stumped.

I am trying to create a link_to item in my view that will not be disabled after being clicked once. It's a page listing people's names and their attendance. The names are colored red if absent and green if present and I'd like the user to be able to toggle the attendance more than once for each person.

Here is the code for my view and controller (note I am using websockets here as well to update the coloring of the button after being clicked):

index.html.erb:

    <div id="reader-attendance-<%= member.id %>">
      <% if member.present == true %>
          <%= link_to member.last_name.upcase, attendance_path(:id => member.id, :present => false), remote: true, class: "btn btn-outline-success btn-14" %>
      <% else %>
        <%= link_to member.last_name.upcase, attendance_path(:id => member.id, :present => true), remote: true, class: "btn btn-outline-danger btn-14" %>
      <% end %>
    </div>

controller.rb:

  def attendance
    member = Member.find(params[:id])
    if(params[:present] == 'true')
      member.present = true
    else
      member.present = false
    end
    member.save
    vote_member = VoteMember.where(member_name: member.display_name)[0]
    if vote_member.vote_type == 'Absent'
      vote_member.vote_type = ''
      vote_member.save
    end
    ActionCable.server.broadcast(
      'attendance_channel',
      {
        member_id: member.id,
        present: params[:present],
        vote_member_id: vote_member.id,
        total_abs: Member.where(present: false).count(),
        member_name: member.last_name.upcase
      }
    )
  end

Solutions I have tried:

  1. I've read that you can add an option to config so that the disable option is turned off as follows: Rails.application.configure do config.action_view.automatically_disable_submit_tag = false end The it seems like this is not supported, however, for the link_to tag and is only supported for button_to

  2. I added the content in #1 to config and then I've tried the button_to option as follows with no success. I have a feeling it's the way that I've passed the parameters in here:

  <%=button_to member.last_name.upcase ,:action => :attendance, :id => member.id, :present => false, remote: true, class: "btn btn-outline-success btn-14" %>

I am working with the following versions:

Rails 6.1.4.1 Ruby 3.0.1

Thanks in advance! Please let me know if other info would be helpful.

Aucun commentaire:

Enregistrer un commentaire