Replacing :id from routes without breaking the convention

我怕爱的太早我们不能终老 提交于 2019-12-13 16:44:58

问题


I just replaced the :id on the resource routes in rails 3 for a hash generated. I'm annoyed by the fact that I'm writing less conventional code (e.g. a link_to edit_user_path(@user) is now written as link_to '/users/#{@user.user_hash}'). So I'm wondering, is therea smarter way to do this?


回答1:


class User < ActiveRecord::Base
  def to_param
    user_hash
  end
end

and edit_user_path(@user) will work properly for you



来源:https://stackoverflow.com/questions/5146286/replacing-id-from-routes-without-breaking-the-convention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!