作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
给定像 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/
我是一名优秀的程序员,十分优秀!