gpt4 book ai didi

ruby-on-rails - routes.rb,如何为路径设置不同的主键?

转载 作者:数据小太阳 更新时间:2023-10-29 06:54:23 26 4
gpt4 key购买 nike

给定像 Thread (id, uuid) 这样的模型,uuid 是一个唯一生成的标识符。我想更改默认路由:

edit_thread GET    /threads/:id/edit(.:format)                        {:action=>"edit", :controller=>"threads"}
thread GET /threads/:id(.:format) {:action=>"show", :controller=>"threads"}
PUT /threads/:id(.:format) {:action=>"update", :controller=>"threads"}

不使用 :id 而是使用用户 :uuid --- 这在 Rails/routes.rb 中是如何实现的?

谢谢

最佳答案

如果我没理解错的话,您想确保 Rails 在路由中使用 :uuid 字段而不是 :id 字段。

这很容易实现,在你的模型中否决 to_param 方法:

def Thread
def to_param
uuid
end
end

在你的 Controller 中,你必须写这样的东西:

thread = Thread.find_by_uuid(params[:id])

希望这对您有所帮助。

关于ruby-on-rails - routes.rb,如何为路径设置不同的主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6836509/

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