微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

给定像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中实现这一点?

谢谢

解决方法

如果我理解正确,你想确定而不是:id字段,Rails使用路由中的:uuid字段.

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

def Thread
  def to_param
    uuid
  end
end

在你的控制器里,你必须写下这样的东西:

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

希望这可以帮助.

原文地址:https://www.jb51.cc/ruby/270199.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐