繁体   English   中英

将JavaScript更改保存到Rails Server

[英]Saving Javascript Changes to Rails Server

页面使用此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">

我的订单

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

我知道如何使用迁移将不同的属性添加到订单。 更改背景颜色时,我希望将更改保存到服务器(以相同方式将文本字段中的就地编辑保存到服务器)。 请帮助我了解如何执行此操作。 谢谢。

您首先需要在rails控制器中执行更新操作,这可能是OrdersController。

然后,在javascript中,您可以使用.ajax()函数对状态为(然后再次为我猜)新的orders /(order_id)进行PUT或PATCH HTTP请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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