【发布时间】:2021-08-06 19:05:57
【问题描述】:
我有一个 Rails 6 应用程序,我正在与浏览器交互以从 Chrome 扩展程序获取一些信息 (getAccount)。
在最后一行,我输入了console.log(account),它会打印出正确的值。但是,我想将帐户变量保存到我的数据库中。我将如何将 account 变量暴露给 Rails,以便将其发送到控制器并保存?
<%= link_to "Home", root_path %>
<button class="enableEthereumButton">Connect MetaMask</button>
<h2>Account: <span class="showAccount"></span></h2>
<script>
const ethereumButton = document.querySelector('.enableEthereumButton');
const showAccount = document.querySelector('.showAccount');
ethereumButton.addEventListener('click', () => {
getAccount();
});
async function getAccount() {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
showAccount.innerHTML = account;
console.log(account)
}
</script>
【问题讨论】:
-
您的意思是告诉您的服务器有关客户端的某些内容?通过例如向您的服务器发出 HTTP 请求?
-
理想情况下将
account的值传递给Rails 控制器。 -
Fetch/Ajax 或传统表单发布。您必须将数据返回到服务器。