目前正在使用Rails中的应用程序,用户接受/拒绝来自其他用户的请求。我的数据库中有一个布尔值from Crypto.Hash import MD5
from Crypto.Cipher import DES
import base64
import re
_password = b'q1w2e3r4t5y6'
_salt = b'\x80\x40\xe0\x10\xf8\x04\xfe\x01'
_iterations = 50
plaintext_to_encrypt = 'MyP455w0rd'
# Pad plaintext per RFC 2898 Section 6.1
padding = 8 - len(plaintext_to_encrypt) % 8
plaintext_to_encrypt += chr(padding) * padding
if "__main__" == __name__:
"""Mimic Java's PBEWithMD5AndDES algorithm to produce a DES key"""
hasher = MD5.new()
hasher.update(_password)
hasher.update(_salt)
result = hasher.digest()
for i in range(1, _iterations):
hasher = MD5.new()
hasher.update(result)
result = hasher.digest()
encoder = DES.new(result[:8], DES.MODE_CBC, result[8:16])
encrypted = encoder.encrypt(plaintext_to_encrypt)
print (str(base64.b64encode(encrypted),'utf-8'))
decoder = DES.new(result[:8], DES.MODE_CBC, result[8:])
d = str(decoder.decrypt(encrypted),'utf-8')
print (re.sub(r'[\x01-\x08]','',d))
,其默认值为false,如果用户点击"接受"我试图将值更改为true。按钮。
我知道我必须制作一个可以发布POST / PATCH请求的表单,但我不确定如何更改该值。
答案 0 :(得分:1)
我知道我必须制作一个可以进行POST / PATCH的表单 请求,但我不确定如何更改该值。
您不需要表单。由于您只是尝试使用单击按钮更新模型实例,因此您只需要link_to
并定义route
来执行此操作。考虑到您的模型为Request
,以下内容应该有效
<%= link_to "Accept", accept_request_path(request) %>
request
是需要更新的实例。
#routes.rb
patch /requests/:id/accept, to: requests#accept, as: :accept_request
或者如果您想要资源丰富的路线,您可以
resources :requests do
patch 'accept', on: :member
end
在requests_controller
中有一个accept
方法,代码如下
def accept
@request = Request.find(params[:id])
@request.update_attribute(accepted: true)
end