gpt4 book ai didi

ruby - 使用自定义主 ID 在 rails3 中进行路由

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:02 24 4
gpt4 key购买 nike

我有带有自定义主键的模型:

文档.rb

class Document < ActiveRecord::Base
set_primary_key "token"
end

路线.rb:

MyApp::Application.routes.draw do
resources :documents, :only => [:index, :show, :create]
end

当我创建新文档时,出现错误:

No route matches {:controller=>"documents", :id=>#<Document id: "b430cfe73aaa5235fbfe", token: "b430cfe73aaa...

当我切换到使用 :id 作为主键时,一切正常。但是我需要使用 token 。

我使用:rails 3.0.0 和 ruby​​ 1.8.7(2010-04-19 补丁级别 253)[i686-linux],MBARI 0x8770,Ruby 企业版 2010.02

感谢您的帮助。

最佳答案

尝试添加到document.rb

def to_param
token
end

关于ruby - 使用自定义主 ID 在 rails3 中进行路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3768740/

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