1

私はこのRailscastに従って、新しいテストアプリにアクティブマーチャントを実装してきましたが、うまくいっていましたが、今はメインアプリに作成したものを追加しようとしています。

テストアプリで[カートに追加]をクリックすると、現在のカートにリダイレクトされ、期待どおりにアイテムが一覧表示されます。

メインアプリで[カートに追加]リンクをクリックすると、次の場所にリダイレクトされます。

http://mainapp.dev/line_items?product_id=1

line_itemsコントローラーは次のようになります。

class LineItemsController < ApplicationController
  def create
    @product = Product.find(params[:product_id])
    @line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
    flash[:notice] = "Added #{@product.name} to cart."
    redirect_to current_cart_url
  end
end

カートに追加リンクは次のようになります。

<%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post, :class => "product_actions" %>

編集-ログ

アイテムの追加に関するテストバージョン(動作中):

Started POST "/line_items?product_id=5" for 127.0.0.1 at 2011-09-01 07:33:27 +0100
  Processing by LineItemsController#create as HTML
  Parameters: {"authenticity_token"=>"li7gkjksc9MENevuGz7emDwnbB6HrvPAE3CY=", "product_id"=>"5"}
  [1m[35mProduct Load (0.4ms)[0m  SELECT `products`.* FROM `products` WHERE `products`.`id` = 5 LIMIT 1
  [1m[36m (33.1ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (179.4ms)[0m  INSERT INTO `carts` (`created_at`, `purchased_at`, `updated_at`) VALUES ('2011-09-01 06:33:28', NULL, '2011-09-01 06:33:28')
  [1m[36m (48.3ms)[0m  [1mCOMMIT[0m
  [1m[35m (0.2ms)[0m  BEGIN
  [1m[36mSQL (0.2ms)[0m  [1mINSERT INTO `line_items` (`cart_id`, `created_at`, `product_id`, `quantity`, `unit_price`, `updated_at`) VALUES (29, '2011-09-01 06:33:29', 5, 1, 250, '2011-09-01 06:33:29')[0m
  [1m[35m (0.5ms)[0m  COMMIT
Redirected to http://sell.dev/cart
Completed 302 Found in 1265ms


Started GET "/cart" for 127.0.0.1 at 2011-09-01 07:33:29 +0100
  Processing by CartsController#show as HTML
  [1m[36mCart Load (0.2ms)[0m  [1mSELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 29 LIMIT 1[0m
  [1m[35mLineItem Load (0.3ms)[0m  SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`cart_id` = 29
  [1m[36mProduct Load (0.5ms)[0m  [1mSELECT `products`.* FROM `products` WHERE `products`.`id` = 5 LIMIT 1[0m
Rendered carts/show.html.erb within layouts/application (202.1ms)
Rendered layouts/_header.html.erb (0.8ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 368ms (Views: 284.2ms | ActiveRecord: 79.5ms)

メインアプリのバージョン:

Started GET "/line_items?product_id=1" for 127.0.0.1 at 2011-09-01 07:34:59 +0100
  Processing by LineItemsController#index as HTML
  Parameters: {"product_id"=>"1"}
Rendered line_items/index.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (1.3ms)
Rendered layouts/_footer.html.erb (178.4ms)
Completed 200 OK in 218ms (Views: 182.3ms | ActiveRecord: 35.5ms)

作成せずに広告申込情報のインデックスにリダイレクトする理由がわかりません。コードは同じです。

編集-ルート

  get "cart" => "carts#show", :as => "current_cart"

  resources :orders
  resources :line_items
  resources :carts    
  resources :products
  resources :order_transactions

編集-私のアプリケーションコントローラーから取得

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def current_cart
    if session[:cart_id]
      @current_cart ||= Cart.find(session[:cart_id])
      session[:cart_id] = nil if @current_cart.purchased_at
    end
    if session[:cart_id].nil?
      @current_cart = Cart.create!
      session[:cart_id] = @current_cart.id
    end
    @current_cart
  end

助けてくれてありがとう!

4

1 に答える 1

1

[カートに追加]リンクをクリックしてカートにアイテムを追加する場合は、現在のようにLineItemsControllerコントローラーで作成アクションを呼び出す必要があります。

そのメソッドの最後の行は

redirect_to current_cart_url

つまり、実際には必要に応じてcurrent_cartにリダイレクトしていますが、実際には意味のない現在のカートにリダイレクトしていないと言っています。

おそらく、current_cart_urlパスやビューなどをまだ設定していませんか?

私はあなたの実際の問題が何であるかはっきりしていません

Started GET "/line_items?product_id=1" for 127.0.0.1 at 2011-09-01 07:34:59 +0100
  Processing by LineItemsController#index as HTML
  Parameters: {"product_id"=>"1"}
Rendered line_items/index.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (1.3ms)
Rendered layouts/_footer.html.erb (178.4ms)
Completed 200 OK in 218ms (Views: 182.3ms | ActiveRecord: 35.5ms)

この後何かが起こっているに違いありません!〜それはなんですか?

編集-解決策 申し訳ありませんが、私は完全に明白なことを逃しました。

<%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post, :class => "product_actions" %>

ログファイルに示されているように、POSTリクエストではなくgetリクエストを発行していますStarted GET "/cart" for 127.0.0.1 at 2011-09-01 07:33:29 +0100

カートにアイテムを追加します。これは、サーバーの状態を変更することを意味します。つまり、get(リンク)リクエストではなくPOSTリクエストを使用する必要があるため、フォームを使用する必要があります(button_toがそのフォームを提供します)。ボットのスパイダーを大量に必要としない限り、/クローラーなど...カートに何かを追加する場合は使用する必要があります

<%= button_to 'Add to Cart', line_items_path(:product_id => product) %>
于 2011-09-01T01:22:43.283 に答える