我一直试图弄清楚这个错误大约3个小时,并找到了其他具有相同标题的示例,但是还没有找到任何解决方案..
该网站说: 语法错误,意外的keyword_ensure,期待$ end,在/controllers/carts_controller.rb第17行
respond_to do |format| /////// <-- says here the problem
rails服务器说问题是:
views/carts/show.html.erb:21: syntax error, unexpected keyword_ensure, expecting $end
......这是整个代码:
/controllers/carts_controller.rb
class CartsController < ApplicationController
def index
@carts = Cart.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @carts }
end
end
def show
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access ionvalid cart #{params[:id]}"
redirect_to store_url, :notice => 'Invalid cart'
else
respond_to do |format|
format.html
format.xml { render :xml => @cart }
end
end
end
def new
@cart = Cart.new
respond_to do |format|
format.html
format.xml { render :xml => @cart }
end
end
def edit
@cart = Cart.find(params[:id])
end
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 action: 'show', status: :created, location: @cart }
else
format.html { render action: 'new' }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
def update
@cart = Cart.find(params[:id])
respond_to do |format|
if @cart.update_attributes(params[:cart])
format.html { redirect_to(@cart, :notice => 'Cart was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@cart = current_cart
@cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html {redirect_to(store_url, :notice => 'Your cart is currently empty') }
format.xml { head :ok }
end
end
end
缩进所有代码
观点: 视图/推车/ show.html.erb
<div class="cart_title">Your Cart</div>
<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 'Empty cart', cart, :method => :delete, :confirm => 'Are you sure?' %>
问题出在 views / carts / show.html.erb 上。将购物车更改为 @cart 解决了问题。
感谢您的帮助。