2016-09-06 10 views
0

私はMichael Hartlのチュートリアルに従っており、私はいくつかの問題に遭遇したショッピングカートを作成しました。ruby​​ on rails、cart&current_userで問題

  1. 各ユーザは、異なる「ID」を持つ新しいショッピングカートを作成することができますが、別のユーザーがカートに製品を追加する場合、追加の製品は、CURRENT_USER

    によって代わりにその特定のカートの異なる「ID」の全てカートに追加します
  2. 他のユーザーカートを表示することなく、自分のカートを表示するようにユーザーを制限する方法を教えてください。

ありがとうございました!

user.rbは(ない、それは長くなりますので、完全なコードは、追加:マイケル・ハートルチュートリアルから元のコードのほかに 'has_oneのカートを')

class User < ActiveRecord::Base 
attr_accessor :remember_token, :activation_token, :reset_token 
before_save :downcase_email 
before_create :create_activation_digest 
has_many :orders 
has_one :cart 

cart.rb

class Cart < ActiveRecord::Base 
has_many :line_items, dependent: :destroy 
belongs_to :user 

def add_product(product_id) 
    current_item = line_items.find_by(product_id: product_id) 
     if current_item 
      current_item.quantity += 1 #quantity of line_item, product in cart 
     else 
      current_item = line_items.build(product_id: product_id) 
     end 
    current_item 
end 


def total_price 
    line_items.to_a.sum { |item| item.total_price } 
end 
end 

懸念/ Current_Cart.rb

module CurrentCart 
extend ActiveSupport::Concern 

private 
def set_cart 
    @cart = current_user.cart || current_user.create_cart 
    session[:cart_id] = @cart.id 
end 
end 

line_items_controller.rb

class LineItemsController < ApplicationController 
include CurrentCart 
before_action :set_cart, only: [:create] #before create, execute :set_cart, find(or create) cart 
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.id) 
    if @line_item.save 
    redirect_to current_user.cart 
    else 
    render :new 
    end 
end 

def update 
    if @line_item.update(line_item_params) 
    redirect_to @line_item, notice: 'Line item was successfully updated.' 
    else 
    render :edit 
    end 
end 

def destroy 
@line_item.destroy 
    redirect_to line_items_url, notice: 'Line item was successfully destroyed.' 
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 

carts_controller.rb

class CartsController < ApplicationController 
before_action :set_cart, only: [:edit, :update, :destroy] 
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart 


def show 
    @cart = current_user.cart 
end 

def edit 
end 


def update 
    if @cart.update(cart_params) 
    redirect_to @cart, notice: 'Cart was successfully updated.' 
    else 
    render :edit 
    end 
end 


def destroy 
    @cart.destroy if @cart.id == session[:cart_id] 
    session[:cart_id] = nil 
    redirect_to store_url 
end 

private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_cart 
    @cart = Cart.find(params[:id]) 
end 

# Never trust parameters from the scary internet, only allow the white list through. 
def cart_params 
    params.fetch(:cart, {}) 
end 

def invalid_cart 
    logger.error "Attempt to access invalid cart #{params[:id]}" 
    redirect_to store_url, notice: 'Invalid cart' 
end 
end 
+0

「私はMichael Hartlのチュートリアルに従っており、ショッピングカートを作成しています」というマイケルハートの書籍はショッピングカートを構築することを思い出しません。しかし、これは私に多くのAgile Web Development with Railsの本を思い​​出させます。もしそうなら、「他のユーザーのカートを見ることができないまま、自分のカートを見るだけに制限する方法は?」と、私は結果がどうなるか理解していません。私は私のレポを見て、あなたの 'current_cart.rb'コードは正しいようでした。 – fbelanger

+0

はい、基本的に両方のチュートリアルを統合しています(ユーザサインイン/ michael hartlからのログインとアジャイル開発のeコマース)、例としてid '1'のユーザとしてログインした場合、id '1'のカートを作成しました。私はログアウトし、ID「2」の別のアカウントでもう一度サインインし、ID「2」のカートを作成しましたが、リンクカート/ 1の別のカートにアクセスすると、発生する。あなたが理解してほしい –

答えて

0

イムは、IDを持つユーザーとしてログインした場合に '1'、i 'がidを持つ私のカートを作成1 'である。私はログアウトし、ID「2」の別のアカウントでもう一度サインインし、ID「2」のカートを作成しましたが、リンクカート/ 1の別のカートにアクセスすると、発生する。あなたが理解してほしい -

他の人のカートを見ることができる理由は、コントローラコードによるものです。

showカートに入るたびに、コントローラーはコントローラ内からset_cartを使用してカートを設定します。

def set_cart 
    @cart = Cart.find(params[:id]) 
end 

これにより、特定のIDを持つカートがすべて取得されます。

次に、渡されたカートが表示されます。

def show 
    @cart = current_user.cart 
end 

あなたがやるべきことは、カートを設定し、コントローラから既存のset_cartを削除するためにcurrent_cart.rbを使用しています。また、current_cart.rbset_cartを公開してください。

またshowルートを変更する必要があります。これは:idと予想されているため、サーバーにどのカートを表示するのかわからないためです。

CurrentCartがどこに含まれているのか正確に忘れていますが、それはApplicationControllerだと思います。もしそうなら、before_action :set_cart, only[...]は他のロジックとうまく動作するはずです。

+0

こんにちは@fbelanger、頭のおかげで、ああそれはちょっと固定されています。 'http:// localhost:3000/cart.84'と表示されますが、どうすれば' http:// localhost:3000/cart'に変更できますか? –

+0

ルートの動作方法を変更し、ERBファイル内のルートを変更する必要があります。 – fbelanger

関連する問題