Twitter Bootstrap Rails button dropdown no responding to AJAX

后端 未结 2 1684
陌清茗
陌清茗 2020-12-19 23:36

I have a Rails 4 app that is using Bootstrap. I am creating a dashboard page that shows buttons that are color coded to represent the status of the tool it represents. The H

相关标签:
2条回答
  • 2020-12-20 00:10

    So I finally figured this one out. The key was having the workflow down: :remote => true connects to the toggle_in_service method in the controller that then calls the toggle_in_service.js.erb file which finally calls toggle_button.html.erb. I also found that when working in Bootstrap it's probably best to try and target an existing <div> for replacement rather than wrap a piece of bootstrap code in another div. Not sure on that but it seemed to matter. Here is the code that worked:

    dashboard.html.erb view snippet:

     <% status_link = tool.in_service ? "Put Out of Service" : "Put In Service" %>
     <% button_link = "#{tool.location}-#{tool.name.singularize}"+'<br>'+" #{tool.serial}" %>
     <div class="btn-group" id="btn<%= tool.id %>" >
    
        <a class="btn custombutton dropdown-toggle <%= (tool.in_service  ? 'btn-success' : 'btn-warning') %>" data-toggle="dropdown" href="#"  >
          <%=  raw(button_link) %>
        </a>
    
        <ul class="dropdown-menu">
          <li><%= link_to status_link, toggle_in_service_path(:id => tool.id), :method => :post, :remote => true %></li>
          <li><%= link_to "view Details", tool_path(tool.id) %></li>
        </ul>
     </div>
    

    This creates a Bootstrap dropdown menu attached to a button that is color coded by the status of the tool.

    The first link in the dropdown menu is a remote ajax call to the toggle_in_service controller, passing the tool.id paramter. The controller method looks like:

    def toggle_in_service
       status_change = @tool.in_service ? false : true
       @tool.update(:in_service => status_change)
       respond_to do |format|
         format.html { redirect_to(@tool) }
         format.js
      end
    end
    

    The boolean value for tool.in_service gets flipped and then calls toggle_in_service.js.erb:

    <% btn_id = "btn#{@tool.id}" %>
    
    $('#<%=btn_id%>').html('<%= escape_javascript(render :partial => "toggle_button") %>');
    

    Which does an AJAX render of the _toggle_button.html.erb partial:

    <% button_link = "#{@tool.location}-#{@tool.name.singularize}"+'<br>'+" #{@tool.serial}" %>
    <% status_link = @tool.in_service ? "Put Out of Service" : "Put In Service" %>
    
    <a class="btn custombutton dropdown-toggle <%= (@tool.in_service  ? 'btn-success' : 'btn-warning') %>" data-toggle="dropdown" href="#"  >
      <%=  raw(button_link) %>
    </a>
    <ul class="dropdown-menu">
      <li><%= link_to status_link, toggle_in_service_path(:id => @tool.id), :method => :post, :remote => true %></li>
      <li><%= link_to "view Details", tool_path(@tool.id) %></li>
    </ul>
    

    Works great! The entire div for the dropdown and the button get updated by AJAX. I'm posting this in case someone looks for something similar. I search many hours on the web and on Stack Overflow, but never found what I needed. I did some reading up on Rails AJAX to figure out what I was doing wrong.

    0 讨论(0)
  • 2020-12-20 00:23

    Probably this is not answer, but I will give my suggestions. First of all you don't need two actions making same things. Try this

    Controller

    def in_service
     @tool.update(:in_service => params[:in_service])
     @success = params[:in_service]
     respond_to do |format|
       format.html { redirect_to(root_path) }
       format.js
       end
    end
    

    In view add parameters to query

    View

    <ul class="dropdown-menu">
        <li><a data-method="post" data-remote="true" href="/update_in_service?id=32&in_service=true" id="in_service" rel="nofollow">In Service</a></li>
        <li><a data-method="post" data-remote="true" href="/update_in_service?id=32&in_service=false" id="out_service" rel="nofollow">Out of Service</a></li>
        <li><a href="/tools/32">view Details</a></li>
    </ul>
    

    Don't need to listen ajax bindings

    js.erb

    <% if @success %>
      $('.btn').addClass('.btn-success').removeClass('.btn-warning');
    <% else %>
        $('.btn').addClass('.btn-warning').removeClass('.btn-success');
    <% end %>
    
    0 讨论(0)
提交回复
热议问题