ruby-on-rails - Rails 6 Production Active Storage NoMethodError(nil:NilClass 的未定义方法“名称”)
问题描述
尝试使用 Active Storage 附加图像时遇到问题它在开发模式下通过,但在生产模式下失败。
详情如下:
模型:
class Post < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
validates :title, presence: true
has_many :post_comments, dependent: :destroy
has_one_attached :wall_picture
# validates :content_rich_text, presence: true
has_rich_text :content_rich_text
def self.approved
where(approved: :true)
end
end
控制器:
def create
@post = Post.new(post_param)
@post.save!
redirect_to posts_path
end
private
# define param for each post
def post_param
params.require(:post).permit(:id, :wall_picture, :title, :content_rich_text)
end
存储配置:
google:
service: GCS
project: pet-app
credentials: <%= Rails.root.join("config/pet-app-google-storage-key.json") %>
bucket: pet-app
登录开发(创建项目成功):
Processing by PostsController#create as HTML
Parameters: {"authenticity_token"=>"IssIp0Mhh90gl9pwuuDfMdrCYz5Rf6gxMlv/WbTe6dAWye25eWAMjfhp135CqGlZj6bCYgvslQHX75yoyn/Sqw==", "post"=>{"wall_picture"=>#<ActionDispatch::Http::UploadedFile:0x00007f05559da830 @tempfile=#<Tempfile:/tmp/RackMultipart20210328-169-j5mnl3.jpg>, @original_filename="cover_building.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[wall_picture]\"; filename=\"cover_building.jpg\"\r\nContent-Type: image/jpeg\r\n">, "title"=>"Test new post", "content_rich_text"=>""}, "commit"=>"Gửi nội dung"}
TRANSACTION (0.2ms) BEGIN
↳ app/controllers/posts_controller.rb:13:in `create'
Post Exists? (0.8ms) SELECT 1 AS one FROM "posts" WHERE "posts"."id" IS NOT NULL AND "posts"."slug" = $1 LIMIT $2 [["slug", "test-new-post"], ["LIMIT", 1]]
↳ app/controllers/posts_controller.rb:13:in `create'
Post Create (3.7ms) INSERT INTO "posts" ("title", "created_at", "updated_at", "slug") VALUES ($1, $2, $3, $4) RETURNING "id" [["title", "Test new post"], ["created_at", "2021-03-28 09:57:04.803702"], ["updated_at", "2021-03-28 09:57:04.803702"], ["slug", "test-new-post"]]
登录生产(我在 Ubuntu 20.04 中使用 Digital Ocean Droplet)
Processing by PostsController#create as HTML
Parameters: {"authenticity_token"=>"d1OTNiyXbvamUEUUUfc9hL1x1Biyh/bJf8NFz99HkTnXNIohTUsn4alNeimiR4o/syG7/NpKCvoSufbVaCp/+A==", "post"=>{"wall_picture"=>#<ActionDispatch::Http::UploadedFile:0x0000563231456050 @tempfile=#<Tempfile:/tmp/RackMultipart20210328-32996-ott8bf.jpg>, @original_filename="cover_building.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[wall_picture]\"; filename=\"cover_building.jpg\"\r\nContent-Type: image/jpeg\r\n">, "title"=>"Test new post", "content_rich_text"=>""}, "commit"=>"Gửi nội dung"}
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms | Allocations: 934)
**NoMethodError (undefined method `name' for nil:NilClass):**
app/controllers/posts_controller.rb:12:in `create'
有人能帮我知道为什么我在生产中遇到这个问题以及如何解决它吗?太感谢了!
解决方案
记得也要配置你的服务config/environments/production.rb
。
# Store files locally.
config.active_storage.service = :local
...:local
或您决定在生产中使用的任何其他服务
https://edgeguides.rubyonrails.org/active_storage_overview.html#disk-service
推荐阅读
- node.js - Sharing memory in NodeJS and Express application
- javascript - 查找并打印网页大小(以字节为单位)和所有标签的计数
- javascript - 带有复选框验证的 Asp.net webform 不起作用
- macos - Ranger 文件管理器 - 在 Preview.APP 中打开 - macOS
- jquery - 悬停时冻结通知并在鼠标离开时继续
- python-3.x - 为什么我无法在 Anaconda 中安装 pylbfgs 包?
- java - 如何获取广播状态 Steam API
- python - 无法通过 Grafana API 导入 grafana 仪表板
- c# - After update of ObservableCollection Xamarin.Forms ListView sends null events
- asp.net - 如何在 HTML 中设置常见的 img src 以跨目录中的 ASPx 页面工作?