为什么在我使用carrierwave将存储更改为cloudinary后,在rails应用程序中上传照片后显示错误?

时间:2017-04-05 04:15:16

标签: ruby-on-rails carrierwave cloudinary

我最近在我的Rails应用程序中将图像的云存储更改为与busrwave集成的cloudinary。它完美有效,但问题是它显示上传图像后照片空白的错误。即使它显示错误,我已手动重新加载根网址,此时照片已上传...问题仅在于新帖子和编辑帖子,但更新个人资料图片和添加个人资料图片没有错误。我使用simple_form上传了照片。我错过了什么,请帮我解决问题。上传照片后的错误页面This is the screenshot


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

  1. 我在cloudinary.yml目录
  2. 中添加了config/
  3. 我在cloudinary中添加了photo_uploader.rb并删除了storage :file
  4. 最后,我在cl前面添加了image_tag标记,并cl_image_tag
  5. 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
    

    我的帖子控制器文件是:

    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
    

    主要问题是执行else create model语句而不是valid。 我怎么解决这个问题请帮助我...

1 个答案:

答案 0 :(得分:0)

  

用户负载(0.4ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; =? ORDER BY&#34;用户&#34;。&#34; id&#34; ASC限制? [[&#34; id&#34;,1],[&#34; LIMIT&#34;,1]]      (0.1ms)开始交易     SQL(0.3ms)

     

INSERT INTO&#34;帖子&#34; (&#34;照片&#34;,&#34;说明&#34;,&#34; user_id&#34;,&#34; created_at&#34;,&#34; updated_at&#34;)价值观(?, ?,?,?,?)[[&#34;照片&#34;,&#34; Screenshot_from_2017-04-07_19-34-57_fxealu.png&#34;],[&#34;说明&#34;,& #34; asd&#34;],[&#34; user_id&#34;,1],[&#34; created_at&#34;,2017-04-07 14:23:14 UTC],[&#34; updated_at&#34;,2017-04-07 14:23:14 UTC]]     SQL(0.2ms)

     

更新&#34;帖子&#34; SET&#34;照片&#34; =&#39; image / upload / v1491574997 / Screenshot_from_2017-04-07_19-34-57_fxealu.png&#39;在哪里&#34;帖子&#34;。&#34; id&#34; =? [[&#34; id&#34;,4]]      (84.2ms)提交事务

您已编制了一篇帖子以验证照片的存在。看一下服务器日志,似乎在验证帖子后更新了照片。

因此flash消息。

从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

希望这会有所帮助。

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