2017-04-05 74 views
0

我最近在我的Rails應用程序中將圖像的雲存儲更改爲與carrierrwave集成的cloudinary。它完美的作品,但問題是,它顯示上傳圖片後照片是空白的錯誤。即使它顯示錯誤,我已經手動重新加載根網址,那時照片已經上傳了......問題只出現在新帖子和編輯帖子中,但更新個人資料圖片和添加個人資料圖片沒有錯誤。我已使用simple_form上傳照片。我錯過了什麼,請幫我解決問題。上傳照片後出現錯誤頁面的This is the screenshot爲什麼使用carrierwave將存儲更改爲cloudinary後,在rails應用中上傳照片後顯示錯誤?


當改變到cloudinary我按照下列步驟:

  1. 我在config/目錄
  2. 我包括在photo_uploader.rbcloudinary除去storage :file加入cloudinary.yml
  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 

我交控制器文件是:

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。 如何解決這個請幫我...

+0

'@ post'上的驗證錯誤是什麼?日誌/提醒'@ post.errors.full_messages'? – Rahul

+0

上傳照片後出現錯誤: 'Woops!看起來有錯誤!' – Achyut

+0

這不是驗證錯誤,而是硬編碼錯誤信息。看着你的截圖,你忘了上傳照片? – Rahul

回答

0

用戶負載(0.4ms)SELECT「users」。* FROM「users」WHERE「users」。「id」=? ORDER BY「users」。「id」ASC LIMIT? [[ 「ID」,1],[ 「限制」,1]] (0.1毫秒)開始事務 SQL(0.3ms的)

INSERT INTO 「職位」( 「照片」, 「描述」,「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.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 

希望這對我有所幫助。

注意:這將允許照片字段爲空。

+0

謝謝兄弟!該解決方案解決了我的問題但是,如果我們想將照片添加爲必填項,我該怎麼辦? – Achyut

相關問題