2017-04-05 1 views
0

최근에 나는 이미지의 구름 저장소를 구름 레일 응용 프로그램의 carrierrwave와 통합 된 클라우드로 변경했습니다. 그것은 완벽하게 작동하지만 문제는 이미지를 업로드 한 후 사진이 비어 있다는 오류를 보여줍니다. 오류가 표시 되더라도 루트 URL을 수동으로 다시로드했습니다. 사진은 이미 업로드되었습니다. 문제는 새 게시물 및 편집 게시물에만 있지만 프로필 사진을 업데이트하고 프로필 사진을 추가해도 오류가 없습니다. simple_form을 사용하여 사진을 업로드했습니다. 내가 무엇을 놓치고 있는지, 문제를 해결하도록 도와주세요. 사진을 업로드 한 후 This is the screenshot 개의 오류 페이지가 표시됩니다.carrierwave를 사용하여 스토리지를 cloudinary로 변경 한 후 레일 앱에 사진을 업로드 한 후 오류가 표시되는 이유는 무엇입니까?


나는 다음과 같은 단계를 따라했습니다 cloudinary 변경 동안 :

  1. 은 내가 photo_uploader.rb에서 cloudinary을 포함 storage :file을 제거 config/ 디렉토리
  2. 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 

가장 큰 문제 그 문은 elsecreate model인데 inste가 실행됩니다. valid의 광고 가 어떻게 "사용자"를 선택

+0

'@ post'의 유효성 검사 오류는 무엇입니까? 로그/경고'@ post.errors.full_messages'? – Rahul

+0

사진 업로드 후 오류 : 'Woops! 오류가있는 것처럼 보입니다! ' – Achyut

+0

유효성 검사 오류가 아니라 하드 코딩 된 오류 메시지입니다. 스크린 샷을 보면서 사진을 업로드하는 것을 잊었습니까? – Rahul

답변

0

사용자로드 (0.4ms)이 저를 도와주세요 해결할 수 있습니다. * "사용자", "사용자". "ID"FROM =? ORDER BY "users". "id"ASC LIMIT? [ "ID"1], [ "LIMIT」1] (는 0.1ms)이"포스트 "("사진 ","설명 ","USER_ID INTO 트랜잭션을 SQL (0.3ms)

INSERT 시작 ","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"= '이미지/업로드/v1491574997/Screenshot_from_2017-04-07_19-34-57_fxealu.png'어디에서 "posts". "id"=? [[ "ID", 4]] (84.2ms) 커밋 트랜잭션

당신은 사진의 존재를 확인이 될 수있는 게시물을 프로그래밍 한. 서버 로그를 살펴보면 게시물의 유효성을 확인한 후 사진이 업데이트 된 것으로 보입니다.

따라서 플래시 메시지.

그것은의 모양은 당신의 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

관련 문제