Reputation: 39
I have issue when try to attach image using Active Storage It passed on development mode, but fail in production mode.
Details as below:
Model:
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
Controller:
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
Storage config:
google:
service: GCS
project: pet-app
credentials: <%= Rails.root.join("config/pet-app-google-storage-key.json") %>
bucket: pet-app
Log in development(create oject success):
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"]]
Log in Production( i'm using Digital Ocean Droplet with Ubuntu 20.04)
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'
Can some one help me known why I get this problem in production and how to solve it? Thank you so much!
Upvotes: 2
Views: 2755
Reputation: 11
Remember to config your service in config/environments/production.rb
too.
# Store files locally.
config.active_storage.service = :local
... :local
or any other service you decide to use in production
https://edgeguides.rubyonrails.org/active_storage_overview.html#disk-service
Upvotes: 1