将Javascript更改保存到Rails服务器

将Javascript更改保存到Rails服务器,javascript,jquery,ruby-on-rails,postgresql,activerecord,Javascript,Jquery,Ruby On Rails,Postgresql,Activerecord,页面使用此javascript函数更改表中行的背景颜色: application.js $(function () { $('.table tr').click(function () { var self = this; var classes = ['new', 'placed', 'pickedUp', 'enroute']; var className; $.each(classes, function(key, val){ i

页面使用此javascript函数更改表中行的背景颜色:

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">
application.js
$(函数(){
$('.table tr')。单击(函数(){
var self=这个;
变量类=['new'、'placed'、'pickedUp'、'inroute'];
var类名;
$.each(类、函数(键、值){
if($(self).hasClass(val)){
className=val;
$(self).removeClass(val);
}
})
var newClass=classes[($.inArray(className,classes)+1)%classes.length];
$(self).addClass(newClass);
});
});
orders.css.scss
.新的{
背景:白色;
}
.放置{
背景:#3498db;
}
皮克多普先生{
背景:#f1c40f;
}
.途中{
背景:#2ecc71;
} 
index.html.erb
我的命令


名称
地点
电话号码
食品
小计
全部的
笔记
 
 
 
 
 
:text区域,
:html_attrs=>{:cols=>'5',:rows=>'1'}
%> 
:text区域,
:html_attrs=>{:cols=>'5',:rows=>'1'}
%> 
:text区域,
:html_attrs=>{:cols=>'20',:rows=>'3'}
%> 


我知道如何使用迁移将不同的属性添加到订单中。更改背景颜色时,我希望将更改保存到服务器(与将文本字段中的最佳就地编辑保存到服务器的方式相同)。请帮我理解怎么做。谢谢。

您首先需要在rails控制器中执行更新操作,即guess is OrdersController

然后,在javascript中,使用.ajax()函数向orders/(order_id)发出一个PUT或PATCH HTTP请求,并使用(我猜也是这样)新状态

<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 %>