0

I am using ruby on rails to upload pics to my website with the paperclip gem, the image uploads but shows that it is broken on the website (i.e does not appear fully)I have done everything know, even searched online for solutions but the image appears broken on my webpage. I don't know what is wrong with it. I have tried everything I know. Any help will be appreciated.

This is the view fornew form page

<%= form_for @pic, :html => { :multipart => true } do |f| %>
  <% if @pic.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@pic.errors.count, "error") %> prohibited this pic from being saved:</h2>

      <ul>
      <% @pic.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>

    <div>
      <%= f.file_field :photo %>
    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

This is my pic controller

class PicsController < ApplicationController
  before_action :set_pic, only: [:show, :edit, :update, :destroy]

  # GET /pics
  # GET /pics.json
  def index
    @pics = Pic.all
  end

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

  # GET /pics/new
  def new
    @pic = Pic.new
  end

  # GET /pics/1/edit
  def edit
  end

  # POST /pics
  # POST /pics.json
  def create
    @pic = Pic.new(pic_params)

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

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

  # DELETE /pics/1
  # DELETE /pics/1.json
  def destroy
    @pic.destroy
    respond_to do |format|
      format.html { redirect_to pics_url, notice: 'Pic was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def pic_params
      params.require(:pic).permit(:name, :description)
    end
end

This is theview for my show page

<p id="notice"><%= notice %></p>

  <%= image_tag @pic.photo.url(:small) %>

<p>
  <strong>Name:</strong>
  <%= @pic.name %>
</p>

<p>
  <strong>Description:</strong>
  <%= @pic.description %>
</p>

<%= link_to 'Edit', edit_pic_path(@pic) %> |
<%= link_to 'Back', pics_path %>

This is my Pics Model

class Pic < ActiveRecord::Base

  has_attached_file :photo,
                    styles: { medium: '300x300#', small: '100x100#', thumb: '25x25#' },
                    default_url: ->(attachment) { 'photo/:style.gif' },
                    convert_options: { all: '-set colorspace sRGB -strip' }

end

In my routes, I just have resources :pics

Alezandah
  • 31
  • 4

1 Answers1

2

You did not include :photo in your trusted parameters. Try it like this:

params.require(:pic).permit(:name, :description, :photo)
Sander Garretsen
  • 1,683
  • 10
  • 19