1

I'm using paperclip to upload images and nested form.

I want to preview images as input, not just an image.

This is my form.

= nested_form_for @anime, html:{multipart:true} do |f|
  - if @anime.errors.any?
    #error_explanation
      %h2= "#{pluralize(@anime.errors.count, "error")} prohibited this anime from being saved:"
      %ul
        - @anime.errors.full_messages.each do |msg|
          %li= msg
  .field
    = f.label :name
    = f.text_area :name
  .fields
    =f.fields_for :images do |i|
      =i.file_field :content
      =i.link_to_remove "Remove"
  .field
    =f.link_to_add "add Image", :images
  .actions
    = f.submit 'Save'

This is my model.

class Image < ApplicationRecord
  belongs_to :imageable, :polymorphic => true, optional:true
  has_attached_file :content, :styles=>{:medium => "300x300>", :thumb => "100x100>"}
  validates_attachment_content_type :content, :content_type => %w(image/jpeg image/jpg image/png)
end

class Anime < ApplicationRecord
  has_many :images, :as => :imageable, dependent: :destroy
  accepts_nested_attributes_for :images, :allow_destroy => true
end

This is my controller

class AnimesController < ApplicationController
  before_action :set_anime, only: [:show, :edit, :update, :destroy]

  # GET /animes
  # GET /animes.json
  def index
    @animes = Anime.all
  end

  # GET /animes/1
  # GET /animes/1.json
  def show
  end

  # GET /animes/new
  def new
    @anime = Anime.new
    @anime.images.build
  end

  # GET /animes/1/edit
  def edit
  end

  # POST /animes
  # POST /animes.json
  def create
    @anime = Anime.new(anime_params)

    respond_to do |format|
      if @anime.save
        format.html { redirect_to @anime, notice: 'Anime was successfully created.' }
        format.json { render :show, status: :created, location: @anime }
      else
        format.html { render :new }
        format.json { render json: @anime.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /animes/1
  # PATCH/PUT /animes/1.json
  def update
    respond_to do |format|
      if @anime.update(anime_params)
        format.html { redirect_to @anime, notice: 'Anime was successfully updated.' }
        format.json { render :show, status: :ok, location: @anime }
      else
        format.html { render :edit }
        format.json { render json: @anime.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /animes/1
  # DELETE /animes/1.json
  def destroy
    @anime.destroy
    respond_to do |format|
      format.html { redirect_to animes_url, notice: 'Anime was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_anime
      @anime = Anime.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def anime_params
      params.require(:anime).permit(:name, images_attributes: [:content])
    end
end

Please help me

Thanks so much.

ignore this message

Bryan Zamora
  • 111
  • 1
  • 1
  • 6
  • Try this http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – PGill Aug 13 '16 at 01:57

0 Answers0