在不违反约定的情况下从路由中替换:id [英] Replacing :id from routes without breaking the convention

查看:86
本文介绍了在不违反约定的情况下从路由中替换:id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将Rails 3中资源路由上的:id替换为生成的哈希。我编写的代码不太传统,这让我很生气(例如,link_to edit_user_path(@user)现在写为link_to‘/users/#{@user.user_hash}’)。所以我想知道,rearea是否更聪明?

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?

推荐答案

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

edit_user_path(@user)将为您正常工作

这篇关于在不违反约定的情况下从路由中替换:id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆