-1

I'm a beginner and I can't solve this problem.

What I did: Setting up Froala WYSIWYG editor with CarrierWave and Rails

Gist: https://gist.github.com/qqnc/c4417aefe120374c8271

Problem: see :picture

#<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>
}

Question: How can I save the uploaded picture without an attachment model? (in 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
Community
  • 1
  • 1
Pascal
  • 1,158
  • 17
  • 20

2 Answers2

1

Easiest way to use WYSIWYG editor in rails and also want to upload images to local server or to Amazon S3 or to Google Cloud Storage is by using CKEditor.

The Ruby Gem Documentation for CKEditor is available at: https://github.com/galetahub/ckeditor.

The detailed installation and usage guide is available at: http://sulmanbaig.com/blog/wysiwyg-editor-for-ruby-on-rails

For carrierwave remember to call rails generate ckeditor:install --orm=active_record --backend=carrierwave after bundle install of the gem.

SulmanWeb
  • 601
  • 6
  • 24
0

In this case, you can just ignore the attachment_controller.rb. I created this, because that was the controller I used to handle ajax call (attachments/upload.json).

However, be sure you have mount_uploader in your TextPost model, and also the PictureUploader is created.[check here in getting started]. Thus, when you save your TextPost, the picture should be saved.

eg:

class TextPost < ActiveRecord::Base
  mount_uploader :picture, PictureUploader
end
Nate Cheng
  • 414
  • 6
  • 11