gpt4 book ai didi

ruby-on-rails - 为未命名为 ":id"的数据库设置主键

转载 作者:行者123 更新时间:2023-12-03 16:01:28 24 4
gpt4 key购买 nike

我正在使用:rails 2.3.5 ruby​​ 1.8.7 和 Windows 7 Home Basic

我得到了一个数据库并将其连接到 rails,从中读取和获取数据没有问题。现在我想做的是在其中添加一些功能(添加、编辑和删除),但是当我尝试通过执行以下代码将主键设置为表的主键 (ProductCode) 时:

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

执行 @products = Product.find(:all, :limit => 10) 时出现此错误:

PosController#index 中的参数错误
参数数量错误(1 代表 0)

我该如何解决这个问题?

这是我的 Controller 代码:
    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

最佳答案

设置主键列的名称。

self.primary_key = "product_code"

关于ruby-on-rails - 为未命名为 ":id"的数据库设置主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com