2013-04-24 117 views
-2

我是Ruby,Rails和一般編程的新手,所以請原諒我,如果問題很微不足道。 我有這樣的觀點:學習Ruby on Rails。語法問題

<h1>Listing products</h1> 

<table> 
    <tr> 
    <th>\</th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% @products.each do |product| %> 
    <tr> 
    <td><%= product.\ %></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 %> 

當我嘗試訪問該鏈接http://localhost:3000/products我收到一個錯誤:

SyntaxError in Products#index 

Showing C:/Sites/depot/app/views/products/index.html.erb where line #13 raised: 

C:/Sites/depot/app/views/products/index.html.erb:13: syntax error, unexpected $undefined 
...tput_buffer.append= (product.\);@output_buffer.safe_concat... 
...        ^
C:/Sites/depot/app/views/products/index.html.erb:18: syntax error, unexpected keyword_end, expecting ')' 
'); end 
    ^
C:/Sites/depot/app/views/products/index.html.erb:25: syntax error, unexpected keyword_ensure, expecting ')' 
C:/Sites/depot/app/views/products/index.html.erb:27: syntax error, unexpected keyword_end, expecting ')' 

Extracted source (around line #13): 

10: 
11: <% @products.each do |product| %> 
12: <tr> 
13:  <td><%= product.\ %></td> 
14:  <td><%= link_to 'Show', product %></td> 
15:  <td><%= link_to 'Edit', edit_product_path(product) %></td> 
16:  <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td> 

Trace of template inclusion: app/views/products/index.html.erb 

我只是按照一本書來學習Rails的一個例子,所以基本上我我沒有做任何事情來編寫這個觀點。 有人能指引我走向正確的方向嗎?

+0

這是什麼'​​<%= product。\%>'?是''產品'的屬性嗎?看起來像你應該調用一些屬性而不是反斜槓 – 2013-04-24 08:28:28

+0

你想要做什麼<% = product。\%>? – Abram 2013-04-24 08:28:43

回答

0

rails會生成非常有用的日誌。一旦它給你的行號和文件名讀一遍。

替換<%= product。\%>與<%= product.name%>其中name是產品的屬性。

+1

不要擔心日誌,安裝better_errors https://github.com/charliesome/better_errors – Abram 2013-04-24 08:55:58

+0

如果我這樣做,我會得到另一個錯誤:'未定義的方法'各爲'nil:NilClass' – user2314555 2013-04-24 09:20:29

4

你需要把<%= product.name %>(在期望的屬性代替),而不是<%= product.\ %>

的一點微小的尖號下的不正確的語法給你什麼絆倒了錯誤的指示。

+0

如果我這樣做,我得到另一個錯誤:'undefined方法'each'for nil:NilClass' – user2314555 2013-04-24 09:19:30

+0

這意味着'@products'爲零,請確保您輸入書籍提供的所有必需代碼。我昨天剛剛完成那個例子,它工作正常。 – 2013-04-24 09:49:19

+0

是的,你需要把@products = Product.all(或其他)在products_controller.rb – Abram 2013-04-24 09:53:46

1

重寫代碼#應用程序\視圖\產品\ index.html.erb」如下:

<h1>Listing products</h1> 

<table> 
<% @products.each do |product| %> 
    <tr class="<%= cycle('list_line_odd', 'list_line_even') %>"> 

    <td> 
     <%= image_tag(product.image_url, class: 'list_image') %> 
    </td> 

    <td class="list_description"> 
     <dl> 
     <dt><%= product.title %></dt> 
     <dd><%= truncate(strip_tags(product.description), 
       length: 80) %></dd> 
     </dl> 
    </td> 

    <td class="list_actions"> 
     <%= link_to 'Show', product %><br/> 
     <%= link_to 'Edit', edit_product_path(product) %><br/> 
     <%= link_to 'Destroy', product, method: :delete, 
        data: { confirm: 'Are you sure?' } %> 
    </td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New product', new_product_path %> 

重寫代碼#應用程序\控制器\ products_controller.rb如下:

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