我最近在我的Rails應用程序中將圖像的雲存儲更改爲與carrierrwave集成的cloudinary。它完美的作品,但問題是,它顯示上傳圖片後照片是空白的錯誤。即使它顯示錯誤,我已經手動重新加載根網址,那時照片已經上傳了......問題只出現在新帖子和編輯帖子中,但更新個人資料圖片和添加個人資料圖片沒有錯誤。我已使用simple_form
上傳照片。我錯過了什麼,請幫我解決問題。上傳照片後出現錯誤頁面的This is the screenshot。爲什麼使用carrierwave將存儲更改爲cloudinary後,在rails應用中上傳照片後顯示錯誤?
當改變到cloudinary我按照下列步驟:
- 我在
config/
目錄 - 我包括在
photo_uploader.rb
cloudinary
除去storage :file
加入cloudinary.yml
。 - 最後,我在
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
我交控制器文件是:
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 model
的聲明else
執行inste廣告valid
。 如何解決這個請幫我...
'@ post'上的驗證錯誤是什麼?日誌/提醒'@ post.errors.full_messages'? – Rahul
上傳照片後出現錯誤: 'Woops!看起來有錯誤!' – Achyut
這不是驗證錯誤,而是硬編碼錯誤信息。看着你的截圖,你忘了上傳照片? – Rahul