开发者

Why I can't loop back the information from db in RoR?

开发者 https://www.devze.com 2022-12-15 21:45 出处:网络
I have a table called \"order\" which has many \"order_items\" , each \"order_items\" is belongs_to\"order\" and \"product\". In the db, I have one record in order. the record is like this:

I have a table called "order" which has many "order_items" , each "order_items" is belongs_to "order" and "product". In the db, I have one record in order. the record is like this:

orders table: id = 1 name= customer

and the order_items table is like this: id=1 product_id=233 order_id =1

id=2 product_id=454 order_id =1

I am trying to this code to show the order, but it is not success:

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

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

and this is my order.rb:

class Order < ActiveRecord::Base
  has_many :order_items



end

last but not least, this is the controller of 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] =开发者_StackOverflow中文版 '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

And this is the error from RoR:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id


Classes in ruby have to be uppercased:

Order # class
order # variable or method

this should work:

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

in your show.html.erb:

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

url:

/orders/:id

if you want, let's say a standard edit form:

# edit.html.erb

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

精彩评论

暂无评论...
验证码 换一张
取 消