我在我的模型中创建了一个方法来验证上传文件类型。但它不起作用,我认为这是因为我使用的是直接上传。
是否可以通过使用活动存储直接上传来验证文件类型?我该怎么办?
我试过:
模型:
validate :correct_video_type
def correct_video_type
if video.attached? && video.content_type.in?(%w(video/mov video/mp4 video/avi video/mpeg))
errors.add(:video, "Must be video format")
elsif video.attached? == false
errors.add(:video, "Video must be attached")
end
end
Run Code Online (Sandbox Code Playgroud)
看法:
<%= form.file_field :video, class: "upload", direct_upload: true %>
Run Code Online (Sandbox Code Playgroud)
什么都没有得到验证......
我也尝试过前端验证,但这不起作用:
<%= form.file_field :video, class: "upload", direct_upload: true, accept: 'video/mov, video/mpeg, video/mp4, video/avi' %>
Run Code Online (Sandbox Code Playgroud)
这些都没有奏效。即使前端工作,我仍然想在后端进一步验证它。
我怎样才能验证这一点?
我正在尝试为 Stripe Elements 存储我的 API 密钥..您可以在此处查看我之前的帖子:Using javascript for Stripe Elements
如您所见,Stripe Elements 没有与来自 Stripe 的 JS 或我的 stripejs.js 文件一起显示。
我假设它的https://js.stripe.com/v3/由于我的 API 密钥没有正确存储而没有以某种方式显示。我目前将 API 密钥存储在由 dotenv gem 管理的 .env 文件中
我正在尝试使用credentials.yml.enc 存储我的API 密钥,但不确定我的操作是否正确。
我所做的:
在 CMD 中调用“EDITOR=atom rails credentials:edit”。原子弹出。我通过以下方式存储 API 密钥:PUBLISHABLE_KEY = pk_test_1111111111111111111 SECRET_KEY = sk_test_11111111111111111111
保存文件。出口。
虽然,它不会工作。我没有正确存储它吗?做错事了吗?
在初始化程序下的我的 stripe.rb 文件中是:
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
Run Code Online (Sandbox Code Playgroud)
编辑:
我的 stripe.rb 文件结构如下:
Rails.configuration.stripe = {
:publishable_key => Rails.application.credentials.stripe[:publishable_key],
:secret_key => Rails.application.credentials.stripe[:secret_key]
} …Run Code Online (Sandbox Code Playgroud) 使用之间有区别吗
validates :foo, uniqueness: true
要么
validates_uniqueness_of :foo?
我知道这是一个简单的问题,但Google没有帮助
什么时候以及为什么要使用另一个?
我正在关注本教程:https://tutorial-extensions.djangogirls.org/en/homework_create_more_models/
我将其添加到我制作的一个简单博客中,以便我可以添加评论
我的错误:
name 'get_object_or_404' is not defined
从这个方法中views.py
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
# post = Post
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('post_detail', pk=post.pk)
else:
form = CommentForm()
return render(request, 'add_comment_to_post.html', {'form': form})
Run Code Online (Sandbox Code Playgroud)
正如您所看到的#hashed out 行。这允许我进入评论视图,但随后我收到错误Cannot assign "<class 'blog.models.Post'>": "Comment.post" must be a "Post" instance.
这是有道理的,但想指出这一点。
我认为这是数据库问题?
我的models.py:
from django.db import models
from django.contrib.auth.models import User
STATUS = (
(0,"Draft"), …Run Code Online (Sandbox Code Playgroud)