简体   繁体   中英

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

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] = '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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM