如何在 Rails 中隐藏查询字符串
How to hidden query-string in Rails
例如,我想在请求中传递给资源
# Go to payment link
<%= link_to 'Payment', checkout_path(pricing: amount.id) %>
当我去付款时 link url 路径是下一个:
http://localhost:3000/checkout?pricing=amount_2aHUHuhdn23jnSJd
我想隐藏查询字符串“pricing=amount_2aHUHuhdn23jnSJd”而不必使用任何 gem
更新问题 31/12
这个请求是Get类型的,因为我需要向用户显示不同的价格,所以参数pass (pricing: amount.id)
<%= link_to 'Payment', checkout_path(pricing: amount.id) %>
get 'checkout', to: 'subscriptions#checkout'
我会感谢你的时间和你的沙粒
没有看到您的 routes.rb 文件,我不太清楚您的意思。正如@Deepak Kumar 提到的,要从 url 中隐藏查询,您应该使用 POST 请求。看看 this guide。您可以在下面添加
post 'payment', to: 'checkout#payment'
在你的 routes.rb 中。这将从您的 CheckoutsController
调用 Payment
操作
当值敏感时,隐藏该值并不能真正解决问题。相反,我建议加密 URL 中的值或使用另一个非敏感值。
- 价值加密
您可以使用 Rails MessageEncryptor
来加密值,然后再将其传递给 URL 并稍后在控制器中再次解密。
# in app/models/url_encrypter.rb
module URLEncrypter
ENCRYPTER = ActiveRecord::MessageEncryptor.new(
Rails.application.secrets.secret_key_base.first(32)
)
def encrypt(value)
ENCRYPTOR.encrypt_and_sign(value, purpose: :url)
end
def decrypt(value)
ENCRYPTOR.decrypt_and_verify(value, purpose: :url)
end
end
# when building the URL
<%= link_to 'Payment', checkout_path(pricing: URLEncrypter.encyrpt(amount.id)) %>
# when reading the param in the controller
pricing = URLEncrypter.decyrpt(params[:pricing])
amount = Amount.find(pricing)
- 有第二个非敏感的唯一标识符
在这里,您向数据库 table 添加了第二个唯一标识符,例如,一个名为 uuid
的列,您可以使用 [=15= 在 before_save
回调中自动填充该列]
然后您可以像这样使用它的值而不是 id
:
# when building the URL
<%= link_to 'Payment', checkout_path(pricing: amount.uuid) %>
# when reading the param in the controller
amount = Amount.find_by(uuid: params[:pricing])
您可以将其存储在 Session。
当用户进入页面时存储它,当用户点击一个link时清除它。
# SomeController#before_payment
session[:pricing] = amount.id
#then..
# CheckoutController#index
pricing = session[:pricing]
session[:pricing] = nil
小心,因为它只会存在于会话中。它将被存储为一个 cookie,并且有 4kb 的数据限制。
例如,我想在请求中传递给资源
# Go to payment link
<%= link_to 'Payment', checkout_path(pricing: amount.id) %>
当我去付款时 link url 路径是下一个:
http://localhost:3000/checkout?pricing=amount_2aHUHuhdn23jnSJd
我想隐藏查询字符串“pricing=amount_2aHUHuhdn23jnSJd”而不必使用任何 gem
更新问题 31/12
这个请求是Get类型的,因为我需要向用户显示不同的价格,所以参数pass (pricing: amount.id)
<%= link_to 'Payment', checkout_path(pricing: amount.id) %>
get 'checkout', to: 'subscriptions#checkout'
我会感谢你的时间和你的沙粒
没有看到您的 routes.rb 文件,我不太清楚您的意思。正如@Deepak Kumar 提到的,要从 url 中隐藏查询,您应该使用 POST 请求。看看 this guide。您可以在下面添加
post 'payment', to: 'checkout#payment'
在你的 routes.rb 中。这将从您的 CheckoutsController
Payment
操作
当值敏感时,隐藏该值并不能真正解决问题。相反,我建议加密 URL 中的值或使用另一个非敏感值。
- 价值加密
您可以使用 Rails MessageEncryptor
来加密值,然后再将其传递给 URL 并稍后在控制器中再次解密。
# in app/models/url_encrypter.rb
module URLEncrypter
ENCRYPTER = ActiveRecord::MessageEncryptor.new(
Rails.application.secrets.secret_key_base.first(32)
)
def encrypt(value)
ENCRYPTOR.encrypt_and_sign(value, purpose: :url)
end
def decrypt(value)
ENCRYPTOR.decrypt_and_verify(value, purpose: :url)
end
end
# when building the URL
<%= link_to 'Payment', checkout_path(pricing: URLEncrypter.encyrpt(amount.id)) %>
# when reading the param in the controller
pricing = URLEncrypter.decyrpt(params[:pricing])
amount = Amount.find(pricing)
- 有第二个非敏感的唯一标识符
在这里,您向数据库 table 添加了第二个唯一标识符,例如,一个名为 uuid
的列,您可以使用 [=15= 在 before_save
回调中自动填充该列]
然后您可以像这样使用它的值而不是 id
:
# when building the URL
<%= link_to 'Payment', checkout_path(pricing: amount.uuid) %>
# when reading the param in the controller
amount = Amount.find_by(uuid: params[:pricing])
您可以将其存储在 Session。
当用户进入页面时存储它,当用户点击一个link时清除它。
# SomeController#before_payment
session[:pricing] = amount.id
#then..
# CheckoutController#index
pricing = session[:pricing]
session[:pricing] = nil
小心,因为它只会存在于会话中。它将被存储为一个 cookie,并且有 4kb 的数据限制。