在销毁数据确认消息中包含变量值

Include value of variable in destroy data confirm message

我想做的是,当我删除一篇文章时,我希望有一条弹出消息说 "Are you sure you want to delete (name of article here)?"

这是我目前的情况。

<% @articles.each do |article| %>
        <tr>
            <td><%= article.title %></td>
            <% @current_title = article.title %>
            <td><%= article.text %></td>
            <td><%= link_to 'Show', article_path(article), id: "submit" %></td>
            <td><%= link_to 'Edit', edit_article_path(article), id: "submit" %></td>
            <td><%= link_to 'Destroy', article_path(article), id: "submit",
                    method: :delete, onclick:"articleConfirm(current_title)",
                    data: { confirm: 'Are you sure?' }%></td>
            <td><a href=#top id="submit">Top</a></td>
        </tr>
<% end %>```

它的作用是显示文章标题、文章内容以及某些选项(编辑、显示、删除或滚动到顶部)。

我已经打印出@current_title 的值,它确实显示了您在索引页中查看的行的当前标题。理想情况下,如果可能的话,我想在确认消息中包含@current_title。

如果你想要一个 Javascript alert 框,当用户想要删除一条消息时,这里你不必包含 onclick:"articleConfirm(@current_title)",只需这样做:

<td><%= link_to 'Destroy', article_path(article), id: "submit",
        method: :delete,
        data: { confirm: "Are you sure you want to delete #{ @current_title } Article?" }%></td>

现在,如果你想要一些模态,我将展示一个 Bootstrap 模态的例子,然后这样做:

<td>
  <button type="button" class="btn btn-outline-danger" 
          data-toggle="modal" data-target="#exampleModal<%= article.id %>">Delete
  </button>
</td>

然后编写模态代码,在该模型中您可以执行如下操作:

<!-- Modal -->
  <div class="modal fade" id="exampleModal<%= article.id %>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-body">
          Are you sure you want to delete <%= article.title %>?
        </div>

        <!-- Delete button in the footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Cancel</button>
          <%= link_to 'Delete', article_path(article), id: "submit", method: :delete, class: 'btn btn-outline-danger' %>
        </div>
      </div>
    </div>
  </div>

如您所见,#exampleModal<%= article.id %> 已添加到 fireup exact article modal。