如何不使用Java脚本删除用户帐户?

时间:2019-01-21 21:28:46

标签: ruby-on-rails ruby

我正在尝试在Rails中创建一个表单,该表单将删除没有Javascript或使用“ method::delete”的用户。我怎样才能做到这一点?当我尝试使用“ users_destroy_path”或“ user_path(@ profile.user)”时,Rails将我重定向到我的Users控制器的“ show”操作,而不是“ destroy”。

表格:

<form action="<%= user_path(@profile.user) %>"" method="delete">
      <input type="submit" value="Destroy">
</form>

2 个答案:

答案 0 :(得分:0)

HTML格式中实际上没有atdiff。 Rails帮助器之所以有用,是因为它们创建了一个隐藏的输入,其中实际上包括DELETE请求表单方法:

method="delete"

您绝对可以自己创建。尽管请确保遵循以下模式。

<input name="_method" type="hidden" value="delete">

这与PUT或PATCH请求相同。他们正在HTML表单上使用POST方法,然后在隐藏的<form method="post" action="/path/to/resource"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="Destroy"> </form> 输入中声明PUT或PATCH。

_method

答案 1 :(得分:0)

您可以通过在控制器中使用destroy方法来做到这一点

uList = document.querySelector('[name =tTable]');

window.onload = function () {
    fetchCall('https://jsonplaceholder.typicode.com/users', getUsers);
    sButton.addEventListener('click', fetchCall.bind(this, 'https://jsonplaceholder.typicode.com/users', sBar), false);
}

function sBar(getObject) {
    let sUser = getObject;
    let inputBar = document.getElementById("searchInput");
    let text = inputBar.textContent;
    let textView = text.toUpperCase();
    for (let i = 0; i < getObject.length; i++) {
        let uObject = sUser[i];
        if (textView == uObject.name || textView ==
            uObject.email) {
            let new_tTable = document.createElement('tbody');
            uList.parentNode.replaceChild(new_tTable, uList)


            let row = uList.insertRow();
            let idInput = document.createElement('td');
            let nameInput = document.createElement('td');
            let usernameInput = document.createElement('td');
            let emailInput = document.createElement('td');
            let cityInput = document.createElement('td');
            let phoneInput = document.createElement('td');
            let websiteInput = document.createElement('td');
            let companyInput = document.createElement('td');

            idInput.textContent = uObject.id;
            nameInput.textContent = uObject.name;
            usernameInput.textContent = uObject.username;
            emailInput.textContent = uObject.email;
            cityInput.textContent = uObject.address.city;
            phoneInput.textContent = uObject.phone;
            websiteInput.textContent = uObject.website;
            companyInput.textContent = uObject.company.name;
            row.appendChild(idInput);
            row.appendChild(nameInput);
            row.appendChild(usernameInput);
            row.appendChild(emailInput);
            row.appendChild(cityInput);
            row.appendChild(phoneInput);
            row.appendChild(websiteInput);
            row.appendChild(companyInput);
        } else {
            alert("User not found");
        }
    }
}


function fetchCall(url, fn) {
    fetch(url)
        .then(function (response) {
            return response.json();
        })
        .then(function (endPoint) {
            fn(endPoint);
        })
        .catch(function (error) {
            console.error(error);
        })
}

function getUsers(getObject) {
    let user = getObject;
    for (let i = 0; i < getObject.length; i++) {
        let userObject = user[i];
        let row = uList.insertRow();
        let idInput = document.createElement('td');
        let nameInput = document.createElement('td');
        let usernameInput = document.createElement('td');
        let emailInput = document.createElement('td');
        let cityInput = document.createElement('td');
        let phoneInput = document.createElement('td');
        let websiteInput = document.createElement('td');
        let companyInput = document.createElement('td');

        idInput.textContent = userObject.id;
        nameInput.textContent = userObject.name;
        usernameInput.textContent = userObject.username;
        emailInput.textContent = userObject.email;
        cityInput.textContent = userObject.address.city;
        phoneInput.textContent = userObject.phone;
        websiteInput.textContent = userObject.website;
        companyInput.textContent = userObject.company.name;
        row.appendChild(idInput);
        row.appendChild(nameInput);
        row.appendChild(usernameInput);
        row.appendChild(emailInput);
        row.appendChild(cityInput);
        row.appendChild(phoneInput);
        row.appendChild(websiteInput);
        row.appendChild(companyInput);
    }
}

在用户模型中

def destroy
  @profil = Profil.find(params[:id])
  @profil.destroy
  redirect_to profil_path(@profil.user.id)
 end

在个人资料模型中

has_many :profils

可见

belongs_to :user