私は初心者で、この問題を解決できません。
私がやったこと: 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 :picture)
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