有一个非常奇怪的问题。我正在关注使用 Rails 进行敏捷 Web 开发(第 2 版),目前正在进行 Ajax 请求迭代。
问题:我的应用程序没有发出 AJAX 请求,而只是重定向到 store_path。
我的订单项创建操作如下所示:
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