-1

我是初学者,我无法解决这个问题。

我做了什么: 使用 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
4

2 回答 2

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,请记住rails generate ckeditor:install --orm=active_record --backend=carrierwave在安装gem 之后调用。

于 2016-03-26T11:18:21.103 回答
0

在这种情况下,您可以忽略 attachment_controller.rb。我创建了这个,因为那是我用来处理 ajax 调用(附件/upload.json)的控制器。

但是,请确保您的 TextPost 模型中有 mount_uploader,并且还创建了 PictureUploader。[在此处查看入门]。因此,当您保存 TextPost 时,应该保存图片。

例如:

class TextPost < ActiveRecord::Base
  mount_uploader :picture, PictureUploader
end
于 2016-10-26T20:35:49.037 回答