Rails Best_In_Place gem不会更新Order表

时间:2014-08-15 07:17:37

标签: ruby-on-rails best-in-place

我正在构建一个市场应用程序。卖家可以列出要销售的产品。我使用best_in_place允许卖家在销售页面中输入跟踪号和运输公司名称。

我也将best_in_place用于另一个模型,并且工作正常。由于某种原因,订单表更新似乎不起作用。这些是我得到的错误:

NoMethodError in Orders#sales
undefined method `tracking' for nil:NilClass

错误指向第一行,下面使用了best_in_place。

以下是我的观点 - 显示了卖家列表和两列输入跟踪和运营商名称:

<div class="center">
  <h2>Sales History for <%= current_user.name %></h2>
</div>

<% if user_signed_in? %>
   <%= link_to 'Add New Listing', new_listing_path, class: "btn btn-link", data: {  no_turbolink: true } %>
<% end %>

<table class="table table-striped table-bordered">
  <tr>
    <th class="col-md-2">Image</th>
    <th class="col-md-2">Item</th>
    <th class="col-md-1">Price</th>
    <th class="col-md-2">Customer</th>
    <th class="col-md-2">Date Sold</th>
    <th class="col-md-2">Shipment Tracking #</th>
    <th class="col-md-1">Carrier (UPS, USPS, etc.)</th>
 </tr>

<% @orders.each do |order| %>
<tr>
  <td><%= image_tag order.listing.image.url(:thumb) %></td>
  <td><%= order.listing.name %></td>
  <td><%= number_to_currency(order.listing.price) %></td>
  <td><%= order.buyer.name %></td>
  <td><%= order.created_at.strftime("%B %-d, %Y") %></td>
  <td><%= best_in_place @order, :tracking, :type => :input %></td>
  <td><%= best_in_place @order, :carrier, :type => :input %></td>
</tr>
<% end %>
</table>

这是我的控制器的销售方法和更新方法。

class OrdersController < ApplicationController
  before_action :set_order, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user! 
  before_action :check_user, only: [:edit, :update]    

def sales
   @orders = Order.all.where(seller: current_user).order("created_at DESC")
end

def update
  @order = Order.find(params[:id])
  @order.update_attributes(params[:order])
end

def check_user
  if current_user.id != @seller && current_user.name != "admin admin"
    redirect_to root_url, alert: "Sorry, you are not the seller of this listing"
  end
end

1 个答案:

答案 0 :(得分:0)

尝试拼写全部,看看它是否有效:

<%= best_in_place @order, :tracking, :type => :input, :path => {:controller => :sales, :action => :update, :id => order.id} %>

编辑: 我认为错误发生在each do,请尝试以下内容:

<% @orders.each do |order| %>
<tr>
  <td><%= image_tag order.listing.image.url(:thumb) %></td>
  <td><%= order.listing.name %></td>
  <td><%= number_to_currency(order.listing.price) %></td>
  <td><%= order.buyer.name %></td>
  <td><%= order.created_at.strftime("%B %-d, %Y") %></td>
  <td><%= best_in_place order, :tracking, :type => :input %></td>
  <td><%= best_in_place order, :carrier, :type => :input %></td>
</tr>
<% end %>

基本上我刚刚@order交换了order ...