2016-11-11 7 views
1

アプリケーションモデル: カートhas_many :line_items 製品has_many :line_items のLineItembelongs_to :cartbelongs_to :productRails。未定義のメソッドはnilのための `remove_product」:NilClass

同じ製品をカートに追加された場合は、複数回、私はそれを数えます番号カート.rb

def add_product(product) 
    current_item = line_items.find_by(product_id: product.id) 
    if current_item 
     current_item.quantity += 1 
    else 
     current_item = line_items.build(product_id: product.id) 
    end 
    current_item 
    end 

今度は、その数を増減する機能をユーザーに与えたいと考えています。

line_items_controller.rb

def create 
    product = Product.find(params[:product_id]) 
    @line_item = @cart.add_product(product) 

    respond_to do |format| 
     if @line_item.save 
     format.html { redirect_to store_index_url } 
     format.js { @highlited_item = @line_item } 
     format.json { render :show, status: :created, location: @line_item } 
     else 
     format.html { render :new } 
     format.json { render json: @line_item.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

line_item.html.erb:増加うまくいっ

<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td> 

しかし、そううまくいかない減少。私の計画は次のとおりです。

cart.rb(これは、どのように動作するかわからない、ここで以来、未定義のメソッドadd_product火災のエラーアップ):

def remove_product(product) 
    current_item = line_items.find_by(product_id: product.id) 
    if current_item.quantity > 1 
     current_item.quantity -= 1 
    elsif current_item.quantity = 1 
     current_item.destroy 
    end 
    current_item unless current_item.nil 
    end 

line_items_controller.rb

def decrease 
    product = Product.find(params[:product_id]) 
    @line_item = @cart.remove_product(product) 
    end 

完全line_items_controller.rb

class LineItemsController < ApplicationController 
     include CurrentCart 
     before_action :set_cart, only: [:create] 
     before_action :set_line_item, only: [:show, :edit, :update, :destroy] 

     def index 
     @line_items = LineItem.all 
     end 

     def show 
     end 

     def new 
     @line_item = LineItem.new 
     end 

     def edit 
     end 

     def create 
     product = Product.find(params[:product_id]) 
     @line_item = @cart.add_product(product) 

     respond_to do |format| 
      if @line_item.save 
      format.html { redirect_to store_index_url } 
      format.js { @highlited_item = @line_item } 
      format.json { render :show, status: :created, location: @line_item } 
      else 
      format.html { render :new } 
      format.json { render json: @line_item.errors, status: :unprocessable_entity } 
      end 
     end 
     end 

     def update 
     respond_to do |format| 
      if @line_item.update(line_item_params) 
      format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' } 
      format.json { render :show, status: :ok, location: @line_item } 
      else 
      format.html { render :edit } 
      format.json { render json: @line_item.errors, status: :unprocessable_entity } 
      end 
     end 
     end 

     def destroy 
     @line_item.destroy 
     respond_to do |format| 
      format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' } 
      format.json { head :no_content } 
     end 
     end 


     private 
     def set_line_item 
      @line_item = LineItem.find(params[:id]) 
     end 

     def line_item_params 
      params.require(:line_item).permit(:product_id) 
     end 
    end 

remove_productの内容がdecreaseの理由は何ですか?

多くの感謝!

+0

'@カート 'は定義されていません。 'before_action'を使ってそれを設定した場合、そのための' reduce'メソッドが必要です。 – Pavan

答えて

2

ゼロための未定義のメソッド 'remove_product':@cartdecrease方法で定義されないあるためNilClass

エラーです。 @cartbefore_actionに設定したので、decreaseの方法で@cartが利用できるように、以下のように調整する必要があります。

before_action :set_cart, only: [:create, :decrease] 

これが問題を解決します。

なぜadd_productメソッドが機能するのですか?

問題がremove_product又はadd_productではないが、それは@cartdecrease方法で定義されていないです。 @cartcreate方法のために利用可能であるとして、それはエラーをトリガしませんでした。

+0

ああ、それはうまくいくはずです。私はそれを設定することを忘れていた... –

+0

@ J.D。質問の下であなたのコメントの説明を提供するために私の答えを更新しました。チェックしてください。 – Pavan

+0

私は同じ瞬間にそれを考え出しました。答えを投稿:Dありがとう! –

関連する問題