【问题标题】:Rails Best_In_Place gem doesn't update Order tableRails Best_In_Place gem 不更新订单表
【发布时间】: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


    【解决方案1】:

    试着把它全部拼出来,看看它是否有效:

    <%= 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...

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 2023-03-19
      • 2012-11-15
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多