'Carrierwave return nil for image link after uploading. Rails 6.1
Hellow everyone! After clone, My app won't upload photos from PC!
It look like data base saving all photo params, but only for adress return nil.
Photo.last -> #<Photo id: 12, photo: nil, event_id: 59, user_id: 4, created_at: "2022-03-31 06:29:36.880107000 +0000", updated_at: "2022-03-31 06:29:36.880107000 +0000">
but presence :true validation is mode on.
my form.html:
<%= form_for [@event, photo], html: { multipart: true } do |f| %>
<div class="form-group">
<%= f.label :photo %>
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit 'upload', class: 'btn btn-primary' %>
</div>
<% end %>
my photo_uploader.rb:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
if Rails.env.production?
storage :fog
else
storage :file
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_allowlist
%w(jpg jpeg gif png)
end
end
My model Photo.rb:
class Photo < ApplicationRecord
belongs_to :event
belongs_to :user
validates :event, presence: true
validates :user, presence: true
validates :photo, presence: true
mount_uploader :photo, PhotoUploader
scope :persisted, -> { where "id IS NOT NULL"}
end
My photo_controller.rb:
class PhotosController < ApplicationController
before_action :set_event, only: [:create, :destroy]
before_action :set_photo, only: [:destroy]
def create
@new_photo = Photo.new(event: @event)
@new_photo.user = current_user
end
private
def photo_params
params.fetch(:photo, {}).permit(:photo)
end
def set_event
@event = Event.find(params[:event_id])
end
def set_photo
@photo = @event.photos.find(params[:id])
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
