2013-06-02 34 views
1

我刚刚开始通过Rails第4版进行敏捷Web开发,并且在第6章中设置我的第一个应用程序时遇到了SyntaxError。代码如下以及错误。我正在运行Ruby 1.9.3和Rails 3.2.1.3。我试图完全按照这本书,所以我不知道我要去哪里错了。我在this question上看到了类似的错误,但它没有解决我的问题。我不明白这个Rails SyntaxError

下面是产品

<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 %> 

中的index.html这里是我得到

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。\%>行的错误。

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 
+1

你确定这个例子包含'product。\'?我从来没有见过这种使用。如果不那么重要,只要删除这一行。和相应的' \' – Firyn

+0

感谢您的快速响应。他们没有在书中显示代码 - 这是使用Product for generate scaffold命令自动创建的代码。我尝试删除该行,它给了一个“NoMethodError”而不是SyntaxError。编辑以添加:当删除产品\和时,第15行中提到的NoMethodError。 – Chris

+0

您可以在删除这两行文件后显示'Product'的模型文件和错误吗? – Firyn

回答

0

@products.each修改到@product.each。并删除“\”行。