简体   繁体   中英

Saving Javascript Changes to Rails Server

The pages uses this javascript function to change the background color of rows in a table:

application.js

$(function () {
$('.table tr').click(function () {
    var self = this;
        var classes = ['new', 'placed', 'pickedUp', 'enroute'];
    var className; 
    $.each(classes, function(key, val){
        if($(self).hasClass(val)){
            className = val;
            $(self).removeClass(val); 
        }
    })
    var newClass = classes[($.inArray(className, classes) + 1) % classes.length];
    $(self).addClass(newClass);    
});
});

orders.css.scss

.new{
background: white;
}

.placed{
    background: #3498db;
}

.pickedUp{
    background: #f1c40f;
}

.enroute{
    background: #2ecc71;
} 


index.html.erb
<div class = "jumbotron">

My Orders

<table class="table">
  <thead>
    <tr class = "id= row">
      <th>Name &nbsp</th>
      <th>Location &nbsp</th>
      <th>Phone Number &nbsp</th>
      <th>Food &nbsp</th>
      <th>Subtotal</th>
      <th>Total</th>
      <th>Notes</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @orders.each do |order| %>
      <tr>
        <td><%= order.created_at.time.asctime %>&nbsp</td>

        <td><%= order.name %>&nbsp</td>

        <td><%= order.location %>&nbsp</td>

        <td><%= order.phone_number %>&nbsp</td>

        <td><%= order.food %>&nbsp</td>

        <td><%= best_in_place order, 
          :subtotal,
          :type => :textarea,
          :html_attrs => { :cols => '5', :rows => '1' } 
        %>&nbsp</td>

        <td> <%= best_in_place order, 
          :total,
          :type => :textarea,
          :html_attrs => { :cols => '5', :rows => '1' } 
        %>&nbsp</td>

         <td> <%= best_in_place order, 
          :notes,
          :type => :textarea,
          :html_attrs => { :cols => '20', :rows => '3' } 
        %>&nbsp</td>

        <% if Time.now < order.created_at.time + 30.seconds %>
        <td><%= link_to 'Edit', edit_order_path(order) %></td>
        <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Order', new_order_path %>

I know how to add the different attributes to orders using a migration. When the background color is changed, I want the changes to be saved to the server (the same way best-in-place edits in textfields are saved to the server). Please help me understand how to do that. Thanks.

You first need to have an update action in your rails controller, which is guess is OrdersController.

Then, in the javascript, you use the .ajax() function to make a PUT or PATCH HTTP Request to orders/(order_id) with the (then again I guess) new status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM