【问题标题】:ActionController::RoutingError (undefined local variable or method `current_order' for OrderItemsController:Class):ActionController::RoutingError(OrderItemsController:Class 的未定义局部变量或方法 `current_order'):
【发布时间】:2016-06-26 21:36:25
【问题描述】:

我正在 Rails 中建立一个具有特定销售模式的商店。我需要允许用户每 30 天在他的订单中只添加 3 件商品。 30 天计数器应在添加第一个 order_item 时开始。一旦 30 天到期,用户将能够添加 3 个订单。如果 30 天没有过去,例如,用户添加了两个 order_item,他仍然可以在 30 天内再添加一个 order_item。同样,如果用户尝试添加超过 3 个项目以显示错误消息并忽略将 order_items 保存到 current_user 的订单。

我现在在日志中收到此错误:

ActionController::RoutingError (undefined local variable or method current_order' for OrderItemsController:Class): app/controllers/order_items_controller.rb:15:in <class:OrderItemsController>' app/controllers/order_items_controller.rb:1:in `<top (required)>''

相关代码:

order_items_controller.rb

class OrderItemsController < ApplicationController
 def create
     @item = OrderItem.new(order_item_params)
  session[:order_id] = current_order.id

  if @item.save
    respond_to do |format|
      format.js { flash[:notice] = "ORDER HAS BEEN CREATED." } 
    end
  else
    redirect_to root_path
  end 
end

 @order = current_order
      @order_item = @order.order_items.new(order_item_params)
      @order.user_id = current_user.id
      @order.save
      session[:order_id] = @order.id


    end
private

  def order_item_params
  base_params = params.require(:order_item)
                      .permit(:quantity, :product_id, :user_id)
  base_params.merge(order: current_order)
 end

order_item.rb

class OrderItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :order

  validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0 }
  validate :product_present
  validate :order_present
validate :only_3_items_in_30_days

  before_save :finalize

  def unit_price
    if persisted?
      self[:unit_price]
    else
      product.price
    end
  end

  def total_price
    unit_price * quantity
  end

private
  def product_present
    if product.nil?
      errors.add(:product, "is not valid or is not active.")
    end
  end

  def order_present
    if order.nil?
      errors.add(:order, "is not a valid order.")
    end
  end

  def finalize
    self[:unit_price] = unit_price
    self[:total_price] = quantity * self[:unit_price]
  end



  def only_3_items_in_30_days
    now = Date.new
    days_since_first = now - order.first_item_added_at

    if order.order_items.count > 2 && days_since_first < 30
      errors.add(:base, 'only 3 items in 30 days are allowed')
    end
    true      # this is to make sure the validation chain is not broken in case the check fails
  end



end

以及正在提交的表单:

<%= form_for OrderItem.new,  html: {class: "add-to-cart"}, remote: true do |f| %>


        <div class="input-group">
          <%= f.hidden_field :quantity, value: 1, min: 1 %>
          <div class="input-group-btn">
            <%= f.hidden_field :product_id, value: product.id %>
            <%= f.submit "Add to Cart", data: { confirm: 'Are you sure that you want to order this item for current month?'}, class: "btn btn-default black-background white" %>
          </div>
        </div>
      <% end %>
    </div>

【问题讨论】:

    标签: ruby-on-rails model-view-controller


    【解决方案1】:

    你在哪里设置你的current_order

    class OrderItemsController < ApplicationController
     def create
         @item = OrderItem.new(order_item_params)
      session[:order_id] = current_order.id
    
      if @item.save
        respond_to do |format|
          format.js { flash[:notice] = "ORDER HAS BEEN CREATED." } 
        end
      else
        redirect_to root_path
      end 
    end
    
    # I have no idea of what this method ought to accomplish could you try to explain? but this is the immediate cause of the error. There could be others though.
    def method_wrapper
      @order = current_order
      @order_item = @order.order_items.new(order_item_params)
      @order.user_id = current_user.id
      @order.save
      session[:order_id] = @order.id
    end
    private
    
      def order_item_params
        base_params = params.require(:order_item)
                          .permit(:quantity, :product_id, :user_id)
        base_params.merge(order: current_order)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 1970-01-01
      • 2017-01-26
      • 2012-03-23
      • 2018-04-19
      • 2015-01-01
      相关资源
      最近更新 更多