为什么我用carrierwave把存储改成cloudinary后,在rails app上传照片后显示错误?

Why it shows error after uploading photo in rails app after I changed the storage to cloudinary with carrierwave?

我最近在我的 Rails 应用程序中将图像的云存储更改为与 carrierrwave 集成的 cloudinary。它完美地工作,但问题是它显示上传图片后照片为空白的错误。即使它显示错误,我也手动重新加载了根 url,当时照片已经上传了...问题仅在新 post 和编辑 post 但更新配置文件图片和添加头像没有错误。我使用 simple_form 上传了照片。我缺少什么,请帮助我解决问题。 This is the screenshot 上传照片后的错误页面。


在更改为 cloudinary 时,我遵循了以下步骤:

  1. 我在 config/ 目录中添加了 cloudinary.yml
  2. 我在 photo_uploader.rb 中包含 cloudinary 并删除了 storage :file
  3. 最后在image_tag前面加了cl标签,做了cl_image_tag

photo_uploader.rb 文件是:

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

我的 post 控制器文件是:

class PostsController < ApplicationController
    before_action :authenticate_user!, only: [:new, :create]
    before_action :is_owner?, only: [:edit, :update, :destroy]

    def index
      @posts = Post.all.order('created_at DESC').includes(:user, comments: :user).paginate(:page => params[:page], :per_page => 10)
    end

    def new
      @post = Post.new
    end

    def create
      @post = current_user.posts.create(post_params)
      if @post.valid?
        flash[:success] = "Your photo has been successfully posted!"
        redirect_to root_path
      else
        flash[:alert] = "Woops! Looks like there has been an error!"
        render :new, status: :unprocessable_entity
      end
    end

    def edit
      @post = Post.find(params[:id])
    end

    def update
      @post = Post.find(params[:id])
      @post.update(post_params)
      if @post.valid?
        flash[:success] = "Your post has been successfully updated!"
        redirect_to root_path
      else
        flash[:alert] = "Woops! Looks like there has been an error!"
        render :edit, status: :unprocessable_entity
      end
    end

    def show
      @post = Post.find(params[:id])
    end

    def destroy
      @post = Post.find(params[:id])
      @post.destroy
      flash[:success] = "The post was successfully deleted!"
      redirect_to root_path
    end

    private

    def is_owner?
      redirect_to root_path if Post.find(params[:id]).user != current_user
    end

    def post_params
      params.require(:post).permit(:user_id, :photo, :description)
    end
end

主要问题是 create modelelse 语句被执行而不是 valid。 我该如何解决这个问题,请帮助我...

User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] (0.1ms) begin transaction SQL (0.3ms)

INSERT INTO "posts" ("photo", "description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["photo", "Screenshot_from_2017-04-07_19-34-57_fxealu.png"], ["description", "asd"], ["user_id", 1], ["created_at", 2017-04-07 14:23:14 UTC], ["updated_at", 2017-04-07 14:23:14 UTC]] SQL (0.2ms)

UPDATE "posts" SET "photo" = 'image/upload/v1491574997/Screenshot_from_2017-04-07_19-34-57_fxealu.png' WHERE "posts"."id" = ? [["id", 4]] (84.2ms) commit transaction

您已经编写了一个 post 来验证照片的存在。查看服务器日志,似乎在 post 验证后更新了照片。

因此闪现消息。

从您的 post.rb

中删除照片验证

它应该看起来像这样。

class Post < ApplicationRecord


belongs_to :user

has_many :comments, dependent: :destroy

mount_uploader :photo, PhotoUploader

delegate :photo, :name, to: :user, prefix: true

validates :description, :user_id, presence: true

acts_as_votable

end

希望对您有所帮助。

注意:这将允许照片字段为空。