【发布时间】:2014-08-15 07:17:36
【问题描述】:
我正在构建一个市场应用程序。卖家可以列出要销售的产品。我正在使用 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
【问题讨论】:
标签: ruby-on-rails best-in-place