2

免责声明:我对 Ruby on Rails非常陌生。

这是我在尝试生成提要时遇到的错误:

undefined method `title' for nil:NilClass

提取的源代码(在第 2 行附近):

atom_feed do |feed|
    feed.title "Who bought #{@product.title}"

    feed.updated @latest_order.try(:updated_at)

应用程序跟踪:

app/views/products/who_bought.atom.builder:2:in `block in _app_views_products_who_bought_atom_builder__3166274680323093135_70240865825480'
app/views/products/who_bought.atom.builder:1:in `_app_views_products_who_bought_atom_builder__3166274680323093135_70240865825480'

这是我第一次使用 atom_feed - 所以我真的不知道在这里寻找什么。我所拥有的似乎直接来自“使用 Rails 4 进行敏捷 Web 开发”一书我错过了什么吗?

4

3 回答 3

1

应该:

编码:

before_action :
set_product, only: [:show, :edit, :update, :destroy, :who_bought]
于 2015-02-17T09:38:50.723 回答
0

问题出在我没有列出的脚本中 - 在产品控制器中:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render action: 'show', status: :created, location: @product }
      else
        format.html { render action: 'new' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        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.destroy
    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end

  def who_bought
    @product = Product.find(params[:id])
    @latest_order = @product.orders.order(:updated_at).last
    if stale?(@latest_order)
      respond_to do |format|
        format.atom
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :description, :image_url, :price)
    end
end

who_bought 方法,列在私有方法下,我也忘记了 ' @' 符号@latest_order

于 2013-08-08T03:44:20.843 回答
0

您的 @product 变量为零。你定义了吗?特定产品是否存在?尝试将其输出到控制台:logger.info @product.inspect

于 2013-08-08T03:45:38.063 回答