1

非常に奇妙な問題を抱えています。Railsを使用したアジャイルWeb開発(第2版)をフォローしており、現在Ajaxリクエストの反復を行っています。

問題:私のアプリケーションは、代わりにstore_pathにリダイレクトするだけでAJAXリクエストを作成していません。

マイラインアイテムの作成アクションは次のようになります。

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

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to(store_url) }
        format.js
        format.xml  { render :xml => @line_item,
          :status => :created, :location => @line_item }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @line_item.errors,
          :status => :unprocessable_entity }
      end
    end
  end

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

  <%= button_to 'Add to Cart', line_items_path(:product_id => product),remote: true %>

次のフォームを作成します。

<form action="/line_items?product_id=3" class="button_to" data-remote="true" method="post"><div><input type="submit" value="Add to Cart"><input name="authenticity_token" type="hidden" value="euZKvTy2ioSTITELTb06ErJZfSeGBuzoMhUyHROvgjI="></div></form>

また、create.js.rjsをview/line_itemsディレクトリに次の内容で配置しました。

page.replace_html('cart', render(@cart))

編集:サーバーログ
うーん...エラーがあるようです<%= javascript_include_tag :defaults %>

Started GET "/" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Processing by StoreController#index as HTML
  Product Load (0.2ms)  SELECT "products".* FROM "products" ORDER BY title
  Cart Load (0.1ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 22]]
  Rendered store/index.html.erb within layouts/application (7.6ms)
  LineItem Load (0.3ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 22
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 3 ORDER BY title LIMIT 1
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 4 ORDER BY title LIMIT 1
  Rendered line_items/_line_item.html.erb (6.7ms)
  Rendered carts/_cart.html.erb (12.4ms)
Completed 200 OK in 34ms (Views: 29.2ms | ActiveRecord: 1.1ms)
[2013-01-23 17:43:05] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/depot.css?body=1" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Served asset /depot.css - 304 Not Modified (0ms)
[2013-01-23 17:43:05] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/defaults.js" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Served asset /defaults.js - 404 Not Found (6ms)

ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"):
  actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.9) lib/rails/engine.rb:479:in `call'
  railties (3.2.9) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /var/lib/gems/1.9.1/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)


Started GET "/assets/logo.png" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Served asset /logo.png - 304 Not Modified (2ms)
[2013-01-23 17:43:05] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/ruby.jpg" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Served asset /ruby.jpg - 304 Not Modified (0ms)
[2013-01-23 17:43:05] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/rtp.jpg" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Served asset /rtp.jpg - 304 Not Modified (0ms)
[2013-01-23 17:43:05] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/wd4d.jpg" for 127.0.0.1 at 2013-01-23 17:43:05 -0800
Served asset /wd4d.jpg - 304 Not Modified (0ms)
[2013-01-23 17:43:05] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started POST "/line_items?product_id=3" for 127.0.0.1 at 2013-01-23 17:43:35 -0800
Processing by LineItemsController#create as HTML
  Parameters: {"authenticity_token"=>"euZKvTy2ioSTITELTb06ErJZfSeGBuzoMhUyHROvgjI=", "product_id"=>"3"}
  Cart Load (0.1ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 22]]
  Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? ORDER BY title LIMIT 1  [["id", "3"]]
  LineItem Load (0.1ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 22 AND "line_items"."product_id" = 3 LIMIT 1
   (0.1ms)  begin transaction
   (0.2ms)  UPDATE "line_items" SET "quantity" = 28, "updated_at" = '2013-01-24 01:43:35.930200' WHERE "line_items"."id" = 56
   (158.5ms)  commit transaction
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 168ms (ActiveRecord: 159.1ms)


Started GET "/" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Processing by StoreController#index as HTML
  Product Load (0.3ms)  SELECT "products".* FROM "products" ORDER BY title
  Cart Load (0.1ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 22]]
  Rendered store/index.html.erb within layouts/application (6.1ms)
  LineItem Load (0.2ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 22
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 3 ORDER BY title LIMIT 1
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 4 ORDER BY title LIMIT 1
  Rendered line_items/_line_item.html.erb (4.7ms)
  Rendered carts/_cart.html.erb (8.8ms)
Completed 200 OK in 28ms (Views: 23.5ms | ActiveRecord: 1.0ms)


Started GET "/assets/depot.css?body=1" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Served asset /depot.css - 304 Not Modified (0ms)
[2013-01-23 17:43:36] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/defaults.js" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Served asset /defaults.js - 404 Not Found (4ms)

ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"):
  actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.9) lib/rails/engine.rb:479:in `call'
  railties (3.2.9) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /var/lib/gems/1.9.1/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)


Started GET "/assets/ruby.jpg" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Served asset /ruby.jpg - 304 Not Modified (0ms)
[2013-01-23 17:43:36] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/logo.png" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Served asset /logo.png - 304 Not Modified (0ms)
[2013-01-23 17:43:36] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/wd4d.jpg" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Served asset /wd4d.jpg - 304 Not Modified (0ms)
[2013-01-23 17:43:36] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/rtp.jpg" for 127.0.0.1 at 2013-01-23 17:43:36 -0800
Served asset /rtp.jpg - 304 Not Modified (0ms)
[2013-01-23 17:43:36] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
4

1 に答える 1

1

さて、私はエラーを見つけました。エラーメッセージを読み込めませんでした。問題は、javascriptライブラリがロードされていなかったことです。解決策を探している人にとって、問題はフォルダ内javascript_include_tag :allのjavascriptファイルを探すことです。/public/javascript必要なjavascriptファイルをjavascript_include_tag :application含めるには、の下にapplication.jsを含めます/app/assets/javascript/application.js

于 2013-01-24T01:57:22.537 に答える