【问题标题】:how to hide "id" in url using ruby on rails如何在 url 中使用 ruby​​ on rails 隐藏“id”
【发布时间】:2015-11-05 15:43:50
【问题描述】:
controller.rb

@user = User.where(:id => params[:id]).take

html.erb

<a href="/home/layout02_03/<%=@interest.id%>">
 something
</a>

点击一个标签然后 url 是“/home/layout02_03/3”

3 是用户的 id,

但我不想暴露那个“id”

我该怎么做,我正在使用 ruby​​ on rails

【问题讨论】:

标签: ruby-on-rails ruby url


【解决方案1】:

您需要在 User 模型上具有任何其他唯一属性,例如一些随机的 GUID 值,一旦你有了它,就用它来代替 ID:

<a href="/home/layout02_03/<%= @interest.obscure_uniq_identifier %>">

或将其定义为用户的默认 URL 表示:

class User

def to_param
  obscure_uniq_identifier
end

您可以使用以下方法生成一个:

before_create :generate_guid
def generate_guid
  self.obscure_uniq_identifier = SecureRandom.hex
end

【讨论】:

    【解决方案2】:

    对于遇到相同问题的任何人,我还建议您使用像 Visibilize 这样的 gem,它为创建可暴露的友好 ID 提供了更多选项。

    只需安装 gem 并在模型中调用 visibilize

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-12
      • 2022-08-11
      • 2013-01-17
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多