Pawel Nackiewicz
Pawel Nackiewicz

Reputation: 279

Why href in button doesn't redirect to link?

I have a html view and this view displays list of elements. All elements have two buttons (edit and delete). When I click on delete all is working correctly but when I click on edit new view is not displayed. When I want to enter my direct url to edit it, all is working...

This is fragment of my code:

<div class="container">
    <!-- topics -->
    <div class="row topic" th:each="user : ${users}">
        <div class="col-4">
            <p>
                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-at"></i></div>
                <a class="title " th:href="@{'/user/' + ${user.id}}" th:text="${user.email}"></a>
            </p>
        </div>
        <div class="col-4">
            <p>
                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-user"></i></div>
                <a class="title " th:href="@{'/user/' + ${user.id}}" th:text="${user.fullName}"></a>
            </p>
        </div>
        <div class="col-2">
        <form action="#" th:action="@{'/user/drop/{id}'(id=${user.getId()})}" th:method="delete" >
            <input type="hidden" name="_method" value="delete" />
            <button type="submit" class="btn btn-danger">Usuń użytkownika</button>
        </form>
        </div>
        <div class="col-2"></div>
      <!--My failed button-->
            <button type="submit" class="btn btn-warning" th:href="@{'/user/edit/' + ${user.id}}">Edycja</button>
        </div>


    </div>
</div>

Upvotes: 0

Views: 749

Answers (1)

David
David

Reputation: 218867

A <button> has no href attribute. What you're looking for is a "link" (an <a> "anchor" tag to be specific):

<a class="btn btn-warning" th:href="@{'/user/edit/' + ${user.id}}">Edycja</a>

The reason your other button "works" is because it's part of a <form> and it submits that form. It doesn't follow any kind of href attribute. You could also make this button part of a form, but that doesn't appear to be the intended functionality. The intended functionality here is to direct the user to a new page, so a link is appropriate.

Upvotes: 2

Related Questions