添加购物车按钮停止工作?

Add Cart button stopped working?

我正在尝试用 Rails 4 本书完成敏捷 Web 开发。我有一个工作 "Add to Cart button" 和 Ajax 。在我做了更多编码后,购物车因我不知道的原因停止工作。 我猜测我在应用程序控制器中添加的钩子方法:

skip_before_action :authorize is the problem but I am not sure. 

感谢您的宝贵时间和帮助。

这是 application.html.erb 文件:

<!DOCTYPE html>
<head>
<html>
<title>Moussa Books Online Store</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body class="<%= controller.controller_name %>">
<div id="banner">
<%= image_tag("logo.png") %>
<%= @page_title || "Moussa Bookshelf" %>
</div>
<div id="columns">
<div id="side">
<div id="cart">
<% if @cart %>
<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
<%= render @cart %>
<% end %>
<% end %>
</div>
<ul>
<li><a href="http://www....">Home</a></li>
<li><a href="http://www..../faq">Questions</a></li> 
<li><a href="http://www..../news">News</a></li>
<li><a href="http://www..../contact">Contact</a></li>
</ul>
<% if session[:user_id] %>
<ul>
<li><%= link_to 'Orders', orders_path %></li>
<li><%= link_to 'Products', products_path %></li>
<li><%= link_to 'Users', users_path %></li>
</ul>
<%= button_to 'Logout', logout_path, method: :delete %>
<% end %>
</div>
<div id="main">
<%= yield %>
</div>
</div>
</body>
</html>

这是我的商店索引页面:app/views/store/index.html.erb

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% if @counter > 5 %>
<p> You have accessed this page <%= pluralize( @counter, 'time') %> </p> 
<% end %>
<% cache ['store', Product.latest] do %>
<% @products.each do |product| %>
<% cache ['entry', product] do %>
<div class="entry">
<%= link_to image_tag(product.image_url), line_items_path(product_id: product), method: :create %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
<%= button_to 'Add to Cart', line_items_path(product_id: product.id), remote: true %>
</div>
</div>
<% end %>
<% end %>
<% end %>

这是我的部分购物车:ap/views/carts/_cart。html.erb

<h2>Your Cart</h2>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to "Checkout", new_order_path, method: :get %>
<%= button_to 'Empty cart', cart, method: :delete, data: { confirm: 'Are you sure?' } %>

还有我的 line_items 部分:app/views/line_items/_line_item.html.erb

<% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %>&times;</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>

我的购物车控制器:

Class CartsController < ApplicationController
skip_before_action :authorize, only: [:create, :update, :destroy]
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
# GET /carts
# GET /carts.json
def index
@carts = Cart.all
end

# GET /carts/1
# GET /carts/1.json
def show
end

  # GET /carts/new
  def new
  @cart = Cart.new
  end

   # GET /carts/1/edit
     def edit
     end

    # POST /carts
    # POST /carts.json
    def create
    @cart = Cart.new(cart_params)
    respond_to do |format|
    if @cart.save
    format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
    format.json { render :show, status: :created, location: @cart }
  else
    format.html { render :new }
    format.json { render json: @cart.errors, status: :unprocessable_entity }
 end
end
end

# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if @cart.update(cart_params)
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end

# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url }
format.json { head :no_content }
end
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[:cart]
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid Cart'
end
end

和 line_items 控制器:

class LineItemsController < ApplicationController 
include CurrentCart
skip_before_action :authorize only: :create
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
# GET /line_items
# GET /line_items.json
def index
@line_items = LineItem.all
end
# GET /line_items/1
# GET /line_items/1.json
def show
end
# GET /line_items/new
def new
@line_item = LineItem.new
end
# GET /line_items/1/edit
def edit
end
# POST /line_items
# POST /line_items.json
def create
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 { @current_item = @line_item }
session[:counter] = 0
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
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
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
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
@line_item.destroy
respond_to do |format|
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully destroyed.' }
  format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
@line_item = LineItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_item_params
  params.require(:line_item).permit(:product_id)
end
end

最后是我的应用控制器:

class ApplicationController < ActionController::Base
skip_before_action :authorize
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
protected
def authorize
unless User.find_by(id: session[:user_id])
redirect_to login_url, notice: "Please log in"
end
end
end

这是踪迹:

Started GET "/assets/carts.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -070
0
Started GET "/assets/line_items.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/orders.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -07
00
Started GET "/assets/products.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -
0700
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/store.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -07 00
Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2015-03-19 21:56:01   -07 00
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -07 00
Started GET "/assets/jquery-ui/effect.js?body=1" for 127.0.0.1 at 2015-03-19 21: 56:01 -0700
Started GET "/assets/jquery-ui/effect-blind.js?body=1" for 127.0.0.1 at   2015-03- 19 21:56:01 -0700
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/admin.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/carts.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/line_items.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/orders.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01     -070 0
Started GET "/assets/products.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0 700
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0 700
Started GET "/assets/store.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:01 -0700
Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:0 -0700
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2015-03-19 21:56:02 -0700
Started GET "/assets/logo.png" for 127.0.0.1 at 2015-03-19 21:56:02 -0700
Started GET "/assets/cs.jpg" for 127.0.0.1 at 2015-03-19 21:56:02 -0700
Started GET "/assets/rtp.jpg" for 127.0.0.1 at 2015-03-19 21:56:02 -0700
Started GET "/assets/ruby.jpg" for 127.0.0.1 at 2015-03-19 21:56:02 -0700
Started POST "/line_items?product_id=1" for 127.0.0.1 at 2015-03-19 21:56:04 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3:     syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected   tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (59.0ms)

Started POST "/line_items?product_id=1" for 127.0.0.1 at 2015-03-19 21:56:05 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(2.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (56.0ms)

Started POST "/line_items?product_id=1" for 127.0.0.1 at 2015-03-19 21:56:06 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                 ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (84.0ms)
Started POST "/line_items?product_id=1" for 127.0.0.1 at 2015-03-19 21:56:14 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end

Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(2.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (54.0ms)
Started POST "/line_items?product_id=2" for 127.0.0.1 at 2015-03-19 21:56:15 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(2.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (56.0ms)

Started POST "/line_items?product_id=2" for 127.0.0.1 at 2015-03-19 21:56:15 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3:    syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (2.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(2.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (78.0ms)

Started POST "/line_items?product_id=2" for 127.0.0.1 at 2015-03-19 21:56:16 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end

Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (10.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(2.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (59.0ms)
Started POST "/line_items?product_id=3" for 127.0.0.1 at 2015-03-19 21:56:17 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                                ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (54.0ms)
Started POST "/line_items?product_id=3" for 127.0.0.1 at 2015-03-19 21:56:17 -07 00
SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3:   syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create
                              ^):
app/controllers/line_items_controller.rb:3: syntax error, unexpected tIDENTIFIER, expecting keyword_end
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (43.0ms)

您在行项目控制器的第 3 行缺少一个逗号,您不能写:authorize:

class LineItemsController < ApplicationController 
  include CurrentCart
  skip_before_action :authorize, :only => :create

如果你观察你的堆栈跟踪,你可以看到错误,在所有 GET 请求之后它说语法错误然后找到你的文件并且在 line_items_controller.rb 的末尾有一个数字,该数字表示该行语法错误在哪里,这样您就可以更容易地找到错误,通常错误不是语法错误,但是检查堆栈几乎总是可以让您看到应用程序在哪里没有按预期工作 :

SyntaxError (C:/Sites/depot/app/controllers/line_items_controller.rb:3:     syntax error, unexpected tIDENTIFIER, expecting keyword_end
skip_before_action :authorize only: :create