加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:10:13 所属栏目:百科 来源:网络整理
导读:给定像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
给定像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])

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读