我刚开始使用 Rails 进行敏捷 Web 开发,第 4 版,在第 6 章设置我的第一个应用程序时遇到了 SyntaxError。代码和错误如下。我正在运行 Ruby 1.9.3 和 Rails 3.2.1.3。我试图完全按照这本书,所以我不确定我哪里出错了。我在这个问题上看到了类似的错误,但它没有解决我的问题。
这是产品的 index.html
<h1>Listing products</h1>
<table>
<tr>
<th>\</th>
<th>Title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @products.each do |product| %>
<tr>
<td><%= product.\ %></td>
<td><%= product.title %></td>
<td><%= product.description %></td>
<td><%= product.image_url %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Product', new_product_path %>
这是我得到的错误
SyntaxError in Products#index
Showing C:/Sites/work/depot/app/views/products/index.html.erb where line #17 raised:
C:/Sites/work/depot/app/views/products/index.html.erb:17: syntax error, unexpected $undefined
...tput_buffer.append= ( product.\ );@output_buffer.safe_concat...
... ^
C:/Sites/work/depot/app/views/products/index.html.erb:26: syntax error, unexpected keyword_end, expecting ')'
'); end
^
C:/Sites/work/depot/app/views/products/index.html.erb:33: syntax error, unexpected keyword_ensure, expecting ')'
C:/Sites/work/depot/app/views/products/index.html.erb:35: syntax error, unexpected keyword_end, expecting ')'
Extracted source (around line #17):
14:
15: <% @products.each do |product| %>
16: <tr>
17: <td><%= product.\ %></td>
18: <td><%= product.title %></td>
19: <td><%= product.description %></td>
20: <td><%= product.image_url %></td>
Trace of template inclusion: app/views/products/index.html.erb
Rails.root: C:/Sites/work/depot
更新以添加 Product 的模型文件,并根据下面的评论删除 <%= product.\ %> 行的错误。
class Product < ActiveRecord::Base
attr_accessible :\, :description, :image_url, :price, :title
end
NoMethodError in Products#index
Showing C:/Sites/work/depot/app/views/products/index.html.erb where line #15 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #15):
12: <th></th>
13: </tr>
14:
15: <% @products.each do |product| %>
16: <tr>
17:
18: <td><%= product.title %></td>
Rails.root: C:/Sites/work/depot
进一步编辑以添加控制器文件
class ProductsController < ApplicationController
# GET /products
# GET /products.json
def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
# GET /products/1
# GET /products/1.json
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @product }
end
end
# GET /products/new
# GET /products/new.json
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
# GET /products/1/edit
def edit
@product = Product.find(params[:id])
end
# POST /products
# POST /products.json
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render json: @product, status: :created, location: @product }
else
format.html { render action: "new" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.json
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
end