我的rails应用程序中有一个模板,通过单击我主页中的链接,第一次加载时不会执行javascript代码.只有在第一次从链接访问它时才会发生这种情况.我不知道为什么会这样.有帮助吗?这是代码.
主页:
#app/views/static_pages/home.html.erb
#...
<%= link_to "Nofify an absence", new_cancellation_path %>
Run Code Online (Sandbox Code Playgroud)
这是Javascript文件:
//app/assets/javascripts/cancellations_new.js
var validateDateTime = function(){
// some other code that returns true or false
}
$(document).ready(function(){
$("#new_cancellation").submit(function(event){
event.preventDefault();
if (validateDateTime()) {
alert("Some alert");
}
else {
// some more code
}
});
});
Run Code Online (Sandbox Code Playgroud)
以及应该执行javascript的模板:
#app/views/cancellations/new.erb
<%= provide(:title, "Notify an absence")%>
<div class="row">
<div class="span3 offset4">
<h3> Notify an absence </h3>
<%= form_for @cancellation, :id => "new_cancellation" do |f| %>
<%= render 'shared/error_messages' %>
<% # some form elements...%> …Run Code Online (Sandbox Code Playgroud) 我正在使用 rails 5.2 并且我正在尝试使用 Amazon S3 设置 Active Storage。我的应用程序可以完全访问 S3,并且可以将avatar图像附加到user. 但是当我尝试删除头像时,我遇到了以下问题:
> user.avatar.attached? #true`
> user.avatar.purge
S3 Storage (697.9ms) Deleted file from key: Ns1KBRzdgxLNnY31sH72vT5t
S3 Storage (227.0ms) Deleted files by key prefix: variants/Ns1KBRzdgxLNnY31sH72vT5t/
Aws::S3::Errors::AccessDenied: Access Denied
Run Code Online (Sandbox Code Playgroud)
然后,当我检查存储桶时,该文件实际上已被删除,但在数据库中查找Blob和Attachment记录仍然存在。
任何想法为什么会发生这种情况?
编辑 我按照接受的答案的建议对我的 IAM 权限进行了一些更新。这些是我更新的项目:
最后我的策略 json 看起来像这样:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::my-bucket/*", …Run Code Online (Sandbox Code Playgroud)