-2

我是 Ruby、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,所以基本上我没有做任何事情来编写这个视图。有人可以指出我正确的方向吗?

4

3 回答 3

4

您需要放置<%= product.name %>(替换所需的属性)而不是<%= product.\ %>

错误语法下的小插入符号可以指示导致错误的原因。

于 2013-04-24T08:30:30.110 回答
1

重写代码#app\views\products\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 %>

重写代码#app\controllers\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
于 2014-11-04T11:31:02.137 回答
0

rails 会生成非常有用的日志。一旦它为您提供行号和文件名,请阅读它。

将 <%= product.\%> 替换为 <%= product.name %> 其中 name 是产品的属性。

于 2013-04-24T08:52:51.623 回答