【问题标题】:Store hash values in url在 url 中存储哈希值
【发布时间】:2016-06-05 07:33:46
【问题描述】:
Rails 5.0.0.rc1
Ruby 2.2.5 (can update to latest)

我想这是可能的。 I'm making a get request that when a field is selected then the user presses the next button, it goes to another page and that page's url looks like:

http://localhost:3000/food/r/new?utf8=%E2%9C%93&id=2&food=Apple&commit=Next

对我来说,这看起来很难看。是不是这样更好看:

http://localhost:3000/food/r/new/<some_random_short_string/<name-of-page>

我想some_random_short_string 会是一个哈希,然后在控制器中会有类似的东西:

hash = params[:some_random_short_string]
hash[:food] #=> "Apple"
etc...

不知道该怎么做。请大家指点一下?

【问题讨论】:

  • 也许friendly_id gem 可以提供帮助?
  • @seph 不是。我最终可能会在 url 中包含许多字段名称。
  • 散列(使用 MD5 或 SHA1)是单向的。您对原始数据进行哈希处理,但永远无法取回它们。
  • @Aetherus 我想通了,谢谢。为此找到了一个简单的方法。我忘记了获取和发布之间的区别;)

标签: ruby-on-rails ruby


【解决方案1】:

我不会删除我的问题,而是会回答它,因为我有一天会回来。

'get' 在 url 中公开数据,所以我选择了post。不需要哈希,只需使用参数来存储值。

提交页面后,网址将类似于:http://localhost:3000/food/r/new。在控制器中,您使用您的参数:

@selected_food = params[:food]

new.html.erb:

<%= @selected_food %> #=> Apple 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2017-01-22
    • 2011-11-13
    相关资源
    最近更新 更多