我最近在我的Rails应用程序中将图像的云存储更改为与busrwave集成的cloudinary。它完美有效,但问题是它显示上传图像后照片空白的错误。即使它显示错误,我已手动重新加载根网址,此时照片已上传...问题仅在于新帖子和编辑帖子,但更新个人资料图片和添加个人资料图片没有错误。我使用simple_form
上传了照片。我错过了什么,请帮我解决问题。上传照片后的错误页面This is the screenshot。
在改为cloudinary时,我遵循了以下步骤:
cloudinary.yml
目录config/
cloudinary
中添加了photo_uploader.rb
并删除了storage :file
。cl
前面添加了image_tag
标记,并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
主要问题是执行else
create model
语句而不是valid
。
我怎么解决这个问题请帮助我...
答案 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
希望这会有所帮助。
注意:这将允许照片字段为空。