【问题标题】:Rails - Updating records using Best in PlaceRails - 使用 Best in Place 更新记录
【发布时间】:2014-08-16 07:58:56
【问题描述】:

我正在构建一个市场应用程序,我正在尝试使用 Best in Place gem 来允许卖家为他们的每个订单添加一个跟踪号。

我得到一个我无法解决的 NoMethodError。

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

这是来自我的订单控制器的 sn-p

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

这是我的查看页面:

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

在这个问题上停留了一段时间。感谢您对此的任何帮助。

【问题讨论】:

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


    【解决方案1】:

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

    试试:

    <%= best in place order, :tracking, :type => :input %>
    

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

    【讨论】:

    • 谢谢,解决了部分问题。现在错误显示ActiveRecord::RecordNotFound (Couldn't find Order without an ID):。如何在视图中传递订单 ID(在订单模型中称为“id”)?在更新方法中,我添加了@order = Order.find(params[:id]),但我仍然得到同样的错误。
    • 我认为这是您控制器中 @orders 的一个单独问题。尝试使用 Order.where 而不是 Order.all.where
    【解决方案2】:

    我想通了。问题在于,由于我在非 Activerecord 环境中使用 best_in_place(作为具有订单列表的表的一部分),因此我需要显式传递订单 ID。我在 best_in_place 文档中找到了这个 https://github.com/bernat/best_in_place#non-active-record-environments

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

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

    这是我的控制器中的更新方法:

      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
    

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 2012-10-28
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多