【问题标题】:Saving Javascript Changes to Rails Server将 Javascript 更改保存到 Rails 服务器
【发布时间】:2014-04-15 21:57:00
【问题描述】:

页面使用这个 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 %>

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

【问题讨论】:

    标签: javascript jquery ruby-on-rails postgresql activerecord


    【解决方案1】:

    你首先需要在你的 Rails 控制器中有一个更新操作,猜测是 OrdersController。

    然后,在 javascript 中,您使用 .ajax() 函数向订单/(order_id) 发出 PUT 或 PATCH HTTP 请求,并带有(然后我猜是)新状态。

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 2016-10-23
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多