将哈希值存储在 url 中


Rails 5.0.0.rc1
Ruby 2.2.5 (can update to latest)

我想这是可能的。我正在发出一个get请求,即当选择一个字段时,用户按下下一步按钮,它会转到另一个页面,该页面的 url 如下所示:

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...

不知道该怎么做。请问有什么指示吗?

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

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

提交页面后,网址将如下所示:http://localhost:3000/food/r/new 。在控制器中,您可以使用参数:

@selected_food = params[:food]

新.html.erb:

<%= @selected_food %> #=> Apple 

相关内容

最新更新