我是初学者,我无法解决这个问题。
我做了什么: 使用 CarrierWave 和 Rails 设置 Froala WYSIWYG 编辑器
要点: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
附件控制器.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