Skip to content

Instantly share code, notes, and snippets.

@usutani
Last active December 26, 2020 07:52
Show Gist options
  • Save usutani/e5151a47f637724c5527be5093d689aa to your computer and use it in GitHub Desktop.
Save usutani/e5151a47f637724c5527be5093d689aa to your computer and use it in GitHub Desktop.
Rails: Hotwire: button_to(delete)の見た目をlink_toに変更する
module ApplicationHelper
def form_data_window_confirm(message)
{
'data-controller' => 'window_confirm',
'data-window_confirm-message-value' => message,
'data-action' => 'window_confirm#show'
}
end
end
<p id="notice"><%= notice %></p>
<h1>Messages</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @messages.each do |message| %>
<tr>
<td><%= message.title %></td>
<td><%= message.content %></td>
<td><%= link_to 'Show', message %></td>
<td><%= link_to 'Edit', edit_message_path(message) %></td>
<td><%= button_to 'Destroy', message, method: :delete, form: form_data_window_confirm('Are you sure?') %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Message', new_message_path %>
.button_to > input[type="submit"] {
background: none;
border: none;
color: #000;
cursor: pointer;
font-size: 13px;
line-height: 18px;
margin: 0px;
padding: 0px;
text-decoration: underline;
&:hover {
color: #fff;
background-color: #000;
}
}
rails new try_hw_confirm --skip-javascript
cd try_hw_confirm
./bin/bundle add hotwire-rails
./bin/rails hotwire:install
./bin/rails g scaffold message title content:text
./bin/rails db:migrate
import { Controller } from "stimulus"
export default class extends Controller {
//static values = { message: String }
show(event) {
//const message = this.messageValue
const message = event.target.dataset.window_confirmMessageValue
if (window.confirm(message) == false) {
event.preventDefault()
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment