Rails 5 API image uploader with carrierwave-base 64 error

问题内容:

I am having trouble getting an image saved to the database with Rails 5 API and carrierwave-base64. I have read around on stack and the docs and cant find a solution. I am passing the image to the Rails API as a base64 string from angular. The image is definitely being encoded as I get console logs both from angular and rails in the terminal. The image is to be saved as a string, in a column called :image in Media table.

However, when I click save, rails returns a 404, with the message :

ActiveRecord::RecordNotFound (Couldn’t find Medium with ‘id’=):

Any help would be greatly appreciated as I’m completely stumped. Thanks in advance. The code:

image_uploader.rb:

class ImageUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_whitelist
    %w(jpg jpeg gif png)
  end
end

media_controller.rb

class MediaController < ApplicationController
before_action :set_medium, only: [:show, :create, :update, :destroy]
# skip_before_action :authenticate_user!

# GET /media
def index
  @media = Medium.all

  render json: @media
end

# GET /media/1
def show
  render json: @medium
end

# POST /media
def create

# @medium = Medium.new(Uploader.upload(medium_params))
  if @medium.save


    render json: @medium, status: :created, location: @medium
  else
    render json: @medium.errors, status: :unprocessable_entity
  end
end

# PATCH/PUT /media/1
def update
if @medium.update(medium_params)
  render json: @medium
else
  render json: @medium.errors, status: :unprocessable_entity
end
end

# DELETE /media/1
def destroy
  @medium.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_medium
  @medium = Medium.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def medium_params
  params.require(:medium).permit(:title, :body, :image)
end
end

models/medium.rb:

class Medium < ApplicationRecord

 has_and_belongs_to_many :projects, class_name: "Project", join_table: 
 "media_projects"
 has_and_belongs_to_many :pages, class_name: "Page", join_table: 
 "media_pages"
 mount_base64_uploader :image, ImageUploader

end

serializers/medium_serializer.rb:

class MediumSerializer < ActiveModel::Serializer
  attributes :id, :title, :body, :image
  has_many :projects
  has_many :pages
end

Thanks for the help! sorry for the waffle, I’m new to this and this is my first post.

EDIT:

I fixed the issue by removing the :create from the before_action in the user model and then editing the create function to look like this:

def create

uploader = ImageUploader.new


@medium = Medium.new((medium_params))


if @medium.save
  uploader.store!(@medium)

  render json: @medium, status: :created, location: @medium
else
  render json: @medium.errors, status: :unprocessable_entity
end
end

cheers

问题评论:

    
you are not showing enough info here, where is your Uploader, also why did you comment out the first line in create action? Which tutorial are you following Show us the how the data is coming in parameters, better is create a sample git hub repo
    
@gates Thanks for the reply, I commented out the create action to see if that was causing the error and then forgot to uncomment it for this post, sorry about that. I appreciate the feedback, I’ll bear that in mind when I post in the future. I found a fix for the problem after posting, as above ^^.
– MikejdeGroot
3 hours ago

答案:

答案1:

remove :create from your before_action :set_medium

答案评论:

    
Thanks! I did this plus changed the uploader syntax
– MikejdeGroot
3 hours ago

原文地址:

https://stackoverflow.com/questions/47752692/rails-5-api-image-uploader-with-carrierwave-base-64-error

添加评论