Last active
February 6, 2020 00:45
-
-
Save y-temp4/2b17dc1ddc6de8882cb132577da1c5c4 to your computer and use it in GitHub Desktop.
RailsでjQueryを読み込まずにmethod: :deleteのリンクを実現する(Routing Error No route matches [GET] ... の対処法) ref: https://qiita.com/y-temp4/items/2d50feb3ff0d65acdf67
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use this hook to configure devise mailer, warden hooks and so forth. | |
# Many of these configuration options can be set straight in your model. | |
Devise.setup do |config| | |
... | |
# The default HTTP method used to sign out a resource. Default is :delete. | |
config.sign_out_via = :delete # ここをgetにする | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= link_to 'ログアウト', destroy_user_session_path, method: :delete %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= button_to 'ログアウト', destroy_user_session_path, method: :delete %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = () => { | |
class Confirm { | |
constructor(el) { | |
this.message = el.getAttribute('data-confirm') | |
if (this.message) { | |
el.form.addEventListener('submit', this.confirm.bind(this)) | |
} else { | |
console && console.warn('No value specified in `data-confirm`', el) | |
} | |
} | |
confirm(e) { | |
if (!window.confirm(this.message)) { | |
e.preventDefault(); | |
} | |
} | |
} | |
Array.from(document.querySelectorAll('[data-confirm]')).forEach((el) => { | |
new Confirm(el) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= button_to 'ログアウト', destroy_user_session_path, method: :delete, data: { confirm: 'ログアウトしてもよろしいですか?' } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="button_to" method="post" action="/users/sign_out"> | |
<input type="hidden" name="_method" value="delete"> | |
<input type="submit" value="ログアウト"> | |
<input type="hidden" name="authenticity_token" value="(省略)"> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Handles "data-method" on links such as: | |
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a> | |
handleMethod: function(link) { | |
var href = rails.href(link), | |
method = link.data('method'), | |
target = link.attr('target'), | |
csrfToken = rails.csrfToken(), | |
csrfParam = rails.csrfParam(), | |
form = $('<form method="post" action="' + href + '"></form>'), | |
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />'; | |
if (csrfParam !== undefined && csrfToken !== undefined && !rails.isCrossDomain(href)) { | |
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />'; | |
} | |
if (target) { form.attr('target', target); } | |
form.hide().append(metadataInput).appendTo('body'); | |
form.submit(); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment