我不明白这个Rails SyntaxError

时间:2013-06-02 23:54:54

标签: ruby-on-rails ruby ruby-on-rails-3

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

以下是产品的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的模型文件以及删除&lt;%= product。\%&gt;的错误以下每条评论的行。

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 个答案:

答案 0 :(得分:0)

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