2016-06-26 7 views
-1

私は、特定の販売モデルを持つストアを構築しています。私は、ユーザーが30日ごとに自分の注文に3つの項目だけを追加できるようにする必要があります。最初のorder_itemを追加すると、30日カウンタが開始されます。 30日が経過すると、ユーザーは3つの注文を追加することができます。 30日が経過しておらず、例として2つのorder_itemを追加すると、30日以内にもう1つのorder_itemを追加することができます。同様に、ユーザーがエラーメッセージを表示するために3つ以上の項目を追加しようとした場合、order_itemsをcurrent_userの順序に保存することは無視されます。ActionController :: RoutingError(OrderItemsController:Classの未定義のローカル変数またはメソッド `current_order '):

私は今、私のログにこのエラーを取得しています:

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> 

答えて

0

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 
関連する問題