为什么我无法在RoR中回退db中的信息?

时间:2010-01-04 14:38:43

标签: ruby-on-rails ruby

我有一个名为“order”的表,其中有许多“order_items”,每个“order_items”是belongs_to“order”和“product”。在db中,我有一条记录。记录是这样的:

订单表: id = 1 name = customer

并且order_items表是这样的: ID = 1 PRODUCT_ID = 233 order_id = 1

ID = 2 PRODUCT_ID = 454 order_id = 1

我正在尝试使用此代码来显示顺序,但这并不成功:

<% semantic_form_for @order do |f| %>  
  <% f.inputs do %>  
    <%= f.input :name %>  

  <% end %>  
  <%= f.buttons %>  
<% end %>

这是我的订单.rb:

class Order < ActiveRecord::Base
  has_many :order_items



end

最后但并非最不重要的是,这是order.rb的控制器:

class OrderController < ApplicationController
  # GET /orders
  # GET /orders.xml
  def index
    @orders = order.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @orders }
    end
  end

  # GET /orders/1
  # GET /orders/1.xml
  def show
    @order = order.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @order }
    end
  end

  # GET /orders/new
  # GET /orders/new.xml
  def new
    @order = order.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @order }
    end
  end

  # GET /orders/1/edit
  def edit
    @order = order.find(params[:id])
  end

  # POST /orders
  # POST /orders.xml
  def create
    @order = order.new(params[:order])

    respond_to do |format|
      if @order.save
        flash[:notice] = 'order was successfully created.'
        format.html { redirect_to(@order) }
        format.xml  { render :xml => @order, :status => :created, :location => @order }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /orders/1
  # PUT /orders/1.xml
  def update
    @order = order.find(params[:id])

    respond_to do |format|
      if @order.update_attributes(params[:order])
        flash[:notice] = 'order was successfully updated.'
        format.html { redirect_to(@order) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.xml
  def destroy
    @order = order.find(params[:id])
    @order.destroy

    respond_to do |format|
      format.html { redirect_to(orders_url) }
      format.xml  { head :ok }
    end
  end
end

这是RoR的错误:

  

为nil调用id,这会   错误地是4 - 如果你真的   想要id为nil,请使用object_id

1 个答案:

答案 0 :(得分:1)

ruby​​中的类必须是大写的:

Order # class
order # variable or method

这应该有效:

class OrderController < ApplicationController
  def show
    @order = Order.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @order }
    end
  end
  # ...
end
你的show.html.erb中的

#...
<%= @order.name =>
#...

URL:

/orders/:id

如果你愿意,让我们说一个标准的编辑表格:

# edit.html.erb

<% form_for @order do |f| %>  
  <%= f.input :name %>   
  # fields
  <%= f.submit "Place order" %>  
<% end %>