2012-04-03 83 views
3

我使用:导轨2.3.5红宝石1.8.7和Windows 7家庭普通版设置为数据库的主键未命名“:id为”

我得到了一个数据库,我把它连接到铁轨,没有读取数据并从中获取数据时遇到问题现在我要做的就是在它添加一些功能(添加,编辑和删除),但是当我试图通过这样的代码来设置我的主键表的主键(产品代码):

class Product < ActiveRecord::Base 
self.primary_key :ProductCode 
end 

我在PosController#指数

引发ArgumentError 错误的参数数目(1 0)

我怎样才能解决这个问题:这个错误做了@products = Product.find(:all, :limit => 10)什么时候?

这里是我的控制器代码:

class PosController < ApplicationController 

    def index 
     @cards = Card.find(:all) 
     @products = Product.find(:all, :limit => 10) 
    end 

    def new 
     @pro = Product.new 
    end 

    def edit 
    @pro = Product.find(params[:id]) 
    end 

    def update 
    @pro = Product.find(params[:id]) 
    if session[:user_id] 
       @log = "Welcome Administrator!" 
       @logout="logout" 
      else 
       @log = "Admin Log in" 
       @logout="" 
      end 

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

    def create 
    @pro = Product.new(params[:product]) 

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

    def destroy 
    @pro = Product.find(params[:id]) 
    @pro.destroy 

    respond_to do |format| 
    flash[:notice] = 'product was successfully deleted.' 
     format.html { redirect_to(:controller => "pos", :action => "index") } 
     format.xml { head :ok } 
    end 
    end 
end 
+0

刚一说明: “产品代码” 不是一个地道的轨道列名。应该是'product_code'。 – 2012-04-03 03:20:06

+0

但它是我的数据库中的列名。我是否也应该将其重命名为“product_code”? – 2012-04-03 03:21:15

+0

嗯,是的,我会的。它不影响性能。但是它可能会给你带来麻烦,因为Rails鼓吹“约定优于配置”,并假设许多事情。其中之一就是数据库名称在snake_case中。 – 2012-04-03 03:26:22

回答

7

设置主键列的名称。

self.primary_key = “PRODUCT_CODE”

3

self.primary_key返回轨道是什么目前认为是主键,因此不带任何参数。如果要设置主键,请使用self.primary_key = 'blah'

早期版本的rails也支持set_primary_key 'blah',但是这在rails 3.2中已被弃用。