Ruby on rails Rails-使用最佳就地更新记录

Ruby on rails Rails-使用最佳就地更新记录,ruby-on-rails,erb,rails-routing,best-in-place,Ruby On Rails,Erb,Rails Routing,Best In Place,我正在构建一个市场应用程序,在这个应用程序中,我试图使用最合适的gem,允许卖家为他们的每个订单添加一个跟踪号 我有一个无法解决的命名错误 NoMethodError in Orders#sales undefined method `tracking' for nil:NilClass 错误指向视图页面中下面的最佳位置行。此视图页面基于Sales方法(在下面的控制器中),我在该方法中筛选特定卖家的订单 这是我的routes.rb和订单路由。因为订单不需要编辑或销毁,所以我没有创建编辑或删除路

我正在构建一个市场应用程序,在这个应用程序中,我试图使用最合适的gem,允许卖家为他们的每个订单添加一个跟踪号

我有一个无法解决的命名错误

NoMethodError in Orders#sales
undefined method `tracking' for nil:NilClass
错误指向视图页面中下面的最佳位置行。此视图页面基于Sales方法(在下面的控制器中),我在该方法中筛选特定卖家的订单

这是我的routes.rb和订单路由。因为订单不需要编辑或销毁,所以我没有创建编辑或删除路线

  resources :listings do
     resources :orders, only: [:new, :create, :update]
     collection { post :import }
  end
这是我的订单控制器的一个片段

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.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
class OrdersController
这是我的查看页面:

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

形象
项目
价格
顾客
售出日期
装运跟踪#
承运人(UPS、USPS等)
:输入%>
:输入%>

我已经在这上面呆了一段时间了。感谢您在这方面的帮助。

我认为问题在于您在.each方法中调用@order

尝试:

:输入%>

您还需要更改视图中的下一行

我明白了。问题是,由于我在非Activerecord环境中使用了最佳位置(作为订单列表表的一部分),所以我需要显式地传递订单id。我在最好的文档中找到了这个

我为更新操作创建了自定义路由

put 'orderupdate' => "orders#update"
然后,在视图中的do循环中,我使用上面路由的自定义路径,并将订单id传递给该路由

  <% @orders.each do |order| %>
<tr>
  <td><%= order.id %></td>
  <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, :url => orderupdate_path(id: order.id) %> </td>
  <td><%= best_in_place order, :carrier, :type => :input, :url => orderupdate_path(id: order.id) %> </td>
</tr>
<% end %>

希望这对别人有帮助

谢谢,这解决了问题的一部分。现在错误显示为
ActiveRecord::RecordNotFound(在没有ID的情况下找不到订单):
。如何在视图中传递订单id(在订单模型中称为“id”)?在update方法中,我添加了
@order=order.find(params[:id])
,但我仍然得到相同的错误。我认为这是控制器中@orders的另一个问题。尝试使用Order.where而不是Order.all.where
  <% @orders.each do |order| %>
<tr>
  <td><%= order.id %></td>
  <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, :url => orderupdate_path(id: order.id) %> </td>
  <td><%= best_in_place order, :carrier, :type => :input, :url => orderupdate_path(id: order.id) %> </td>
</tr>
<% end %>
  def update
    @order = Order.find(params[:id])
    respond_to do |format|
       if @order.update(order_params)
         format.html { redirect_to sales_url, notice: 'Order updated.' }
         format.json { head :no_content }
       else
         format.html { render action: 'edit' }
         format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end