簡體   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