我是初学者,我无法解决这个问题。
我做了什么: Setting up Froala WYSIWYG editor with CarrierWave and Rails
要点:https://gist.github.com/qqnc/c4417aefe120374c8271
问题: 见:图片
#<TextPost:0x0000000591aaa0> {
:id => 48,
:title => "Alba",
:body => "<p><img class=\"fr-dib\" src=\"/uploads/attachment/picture/6/A_2.png\" style=\"width: 300px;\" data-status=\"OK\"></p>",
:url => nil,
:type => "TextPost",
:user_id => 1,
:created_at => Tue, 23 Feb 2016 20:41:08 UTC +00:00,
:updated_at => Tue, 23 Feb 2016 20:41:08 UTC +00:00,
:comments_count => 0,
:picture => #<PictureUploader:0x000000058db0f8 @model=#<TextPost id: 48, title: "Alba", body: "<p><img class=\"fr-dib\" src=\"/uploads/attachment/pi...", url: nil, type: "TextPost", user_id: 1, created_at: "2016-02-23 20:41:08", updated_at: "2016-02-23 20:41:08", comments_count: 0, picture: nil>, @mounted_as=:picture>
}
问题:如何在没有附件模型的情况下保存上传的图片? (在TextPost:图片中)
text_post_controller.rb
class TextPostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :admin_user, only: :destroy
def new
@text_post = TextPost.new
end
def create
@text_post = current_user.text_posts.build(text_post_params)
if @text_post.save
redirect_to post_path(@text_post),
notice: "Post created!"
else
render :new, alert: "Error creating post."
end
end
def edit
@text_post = current_user.text_posts.find(params[:id])
end
def update
@text_post = current_user.text_posts.find(params[:id])
if @text_post.update(text_post_params)
redirect_to post_path(@text_post),
notice: "Post updated!"
else
render :edit, alert: "Error updating post."
end
end
def destroy
@text_post = TextPost.find(params[:id])
if @text_post.destroy
flash[:success] = "Post deleted."
redirect_to request.referrer || root_url
else
flash[:alert] = "Error deleting post."
redirect_to post_path(@text_post)
end
end
private
def text_post_params
params.require(:text_post).permit(:title, :body, :picture)
end
# Before filters
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
attachments_controller.rb
class AttachmentsController < ApplicationController
def upload
@attachment = Attachment.new
@attachment.picture = params[:file]
@attachment.save
respond_to do |format|
format.json { render :json => { status: 'OK',
link: @attachment.picture.url}
}
end
end
end
答案 0 :(得分:1)
在rails中使用WYSIWYG编辑器以及想要将图像上传到本地服务器或Amazon S3或Google Cloud Storage的最简单方法是使用CKEditor。
CKEditor的Ruby Gem文档位于:https://github.com/galetahub/ckeditor。
详细的安装和使用指南位于:http://sulmanbaig.com/blog/wysiwyg-editor-for-ruby-on-rails
对于carrierwave,请记得在捆绑安装gem之后调用rails generate ckeditor:install --orm=active_record --backend=carrierwave
。
答案 1 :(得分:0)
在这种情况下,您可以忽略attachment_controller.rb。我创建了这个,因为那是我用来处理ajax调用的控制器(attachments / upload.json)。
但是,请确保在TextPost模型中安装了mount_uploader,并且还创建了PictureUploader。[check here in getting started]。因此,当您保存TextPost时,应保存图片。
例如:
class TextPost < ActiveRecord::Base
mount_uploader :picture, PictureUploader
end