Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails更新没有表单的字段_Ruby On Rails_Forms_Updatemodel - Fatal编程技术网

Ruby on rails Rails更新没有表单的字段

Ruby on rails Rails更新没有表单的字段,ruby-on-rails,forms,updatemodel,Ruby On Rails,Forms,Updatemodel,我已经开发了Rails 4.0应用程序,它的“订单”模型有一个状态字段。调用“show”操作以获取“order”对象详细信息时,除了“Edit”和“Back”之外,我还想在“show page”上添加其他按钮。第一个按钮标记为“更新状态”,该按钮仅将此订单对象的状态更新为“1”;第二个按钮标记为“关闭订单”,该按钮仅将此订单对象的状态更新为2。如果没有“编辑”操作/视图,我如何实现这一点?我是否需要使用form_标记来添加这些按钮,是否需要在OrdersController中编写两个新方法来更新

我已经开发了Rails 4.0应用程序,它的“订单”模型有一个状态字段。调用“show”操作以获取“order”对象详细信息时,除了“Edit”和“Back”之外,我还想在“show page”上添加其他按钮。第一个按钮标记为“更新状态”,该按钮仅将此订单对象的状态更新为“1”;第二个按钮标记为“关闭订单”,该按钮仅将此订单对象的状态更新为2。如果没有“编辑”操作/视图,我如何实现这一点?我是否需要使用form_标记来添加这些按钮,是否需要在OrdersController中编写两个新方法来更新状态

下面是我的show.html.erb,有两个按钮我想添加

<p>
  <strong>Name:</strong>
  <%= @order.name %>
</p>
<p>
  <strong>Status:</strong>
  <%= @order.status %>
</p>
<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %> |
<%= link_to 'Shipped', '#', :name => 'shipped_button' %> |
<%= link_to 'Close',   '#', :name => 'close_button' %>
如果我单击“Shipped”或“Close”按钮,它们都将作为“GET”请求被调用到“OrdersController”,如下所示

。。 2014-11-21 15:40:38-0800开始获取/订单/1?名称=127.0.0.1的已发货按钮 按OrdersController处理显示为HTML 参数:{name=>u按钮,id=>1} 订单加载0.1ms从订单中选择订单。*,其中orders.id=?限制1[[id,1]] 渲染订单/show.html.erb在布局/应用程序1.9ms内 在50ms视图中完成200 OK:47.8ms |活动记录:0.1ms


如何使用适当的状态字段更新来更新模型。”“状态”是“订单”模型中的一个整数字段。

我会用ajax实现这一点:

在routes.rb文件中需要类似的内容

订单/show.html.erb:

<div id="order" data-order-id="<%= @order.id %>"></div>

<p id="notice"><%= notice %></p>

<p>
  <strong>Status:</strong>
  <%= @order.status %>
</p>

<p>
  <strong>Name:</strong>
  <%= @order.name %>
</p>

<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %>
<%= link_to 'Shipped', '',id: "ship" %> |
<%= link_to 'Close',   '', id: "close" %>



<script> 
$(function () {
    var orderId = $('#order').data("order-id");
    var shippButton = $('#ship');

    var closeButton = $('#close');
    alert(orderId);



    closeButton.on("click", function() {
        $.ajax({
            url: '/orders/'+orderId,
            type: 'PATCH',
            dataType: 'json',
            data: {"order": { "status": "4"}},
            complete: function (jqXHR, textStatus) {
                // callback
            },
            success: function (data, textStatus, jqXHR) {
                // inform user that it was a sucess like:
                alert("successfully changed the order to closed");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // do something if the request fails
            }
        });
    });

    shippButton.on("click", function() {
        $.ajax({
            url: '/orders/'+orderId,
            type: 'PATCH',
            dataType: 'json',
            data: {"order": { "status": "3"}},
            complete: function (jqXHR, textStatus) {
                // callback
            },  
            success: function (data, textStatus, jqXHR) {
                // inform user that it was a sucess like:
                alert("successfully changed the order to shipped");
            },  
            error: function (jqXHR, textStatus, errorThrown) {
                // do something if the request fails
            }   
        }); 
    });
});

</script>

我想不出你想要什么。真正地你能详细解释一下吗?但是,您可以将操作传递给link_,使其像中所述的那样工作。是的,您指出的答案在这种情况下看起来是可行的。link_to只调用控制器中id或整个对象作为输入参数的操作。@auL5agoi,谢谢,我将尝试此更改并更新我的响应。但由于我以前从未使用过Javascript,我应该将这些JS函数放在show.html.erb中的什么位置?它们是否在某种html标记下?@Atarang啊,好的,你可以将它们包装在一个脚本标记中。这都在show.html.erb文件中。更新了我的答案。顺便说一句,请描述您需要更改哪些属性:例如:x时状态:1,y时状态:2等等。@auL5agoi,我还没有尝试过您的建议。我有一个订单模型,它的状态散列为{:new=>1,:open=>2,:shipped=>3,:close=>4},根据单击“shipped”或“close”,我需要为模型中的“status”字段设置适当的值。为什么你的方法被认为是“坏的”?如果没有JS代码,Rails实现同样目标的方法是什么?@Atarang我不知道Rails的方法-我试图解决问题。您能试试我的例子吗?@auL5agoi,在按照您的建议进行更改之后,我根本看不到ajax函数被调用。在一条语句中,这是如何调用ajax函数的?
<div id="order" data-order-id="<%= @order.id %>"></div>

<p id="notice"><%= notice %></p>

<p>
  <strong>Status:</strong>
  <%= @order.status %>
</p>

<p>
  <strong>Name:</strong>
  <%= @order.name %>
</p>

<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %>
<%= link_to 'Shipped', '',id: "ship" %> |
<%= link_to 'Close',   '', id: "close" %>



<script> 
$(function () {
    var orderId = $('#order').data("order-id");
    var shippButton = $('#ship');

    var closeButton = $('#close');
    alert(orderId);



    closeButton.on("click", function() {
        $.ajax({
            url: '/orders/'+orderId,
            type: 'PATCH',
            dataType: 'json',
            data: {"order": { "status": "4"}},
            complete: function (jqXHR, textStatus) {
                // callback
            },
            success: function (data, textStatus, jqXHR) {
                // inform user that it was a sucess like:
                alert("successfully changed the order to closed");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // do something if the request fails
            }
        });
    });

    shippButton.on("click", function() {
        $.ajax({
            url: '/orders/'+orderId,
            type: 'PATCH',
            dataType: 'json',
            data: {"order": { "status": "3"}},
            complete: function (jqXHR, textStatus) {
                // callback
            },  
            success: function (data, textStatus, jqXHR) {
                // inform user that it was a sucess like:
                alert("successfully changed the order to shipped");
            },  
            error: function (jqXHR, textStatus, errorThrown) {
                // do something if the request fails
            }   
        }); 
    });
});

</script>