coreypizzle
coreypizzle

Reputation: 239

Rails Paperclip - Cannot upload image correctly

I am making a small rails application that requires being able to upload an image to a post/ad and then displaying that image.

I am using the 'Paperclip' gem to handle the image uploads. Whenever I create my post/ad the default 'missing.jpg' is displayed, not my image.

Ads Controller

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # GET /ads
  # GET /ads.json
  def index
    @ads = Ad.all
  end

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

  # GET /ads/new
  def new
    @ad = Ad.new
  end

  # GET /ads/1/edit
  def edit
  end

  # POST /ads
  # POST /ads.json
  def create
    @ad = Ad.new(ad_params)

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

  # PATCH/PUT /ads/1
  # PATCH/PUT /ads/1.json
  def update
    respond_to do |format|
      if @ad.update(ad_params)
        format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ads/1
  # DELETE /ads/1.json
  def destroy
    @ad.destroy
    respond_to do |format|
      format.html { redirect_to ads_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def ad_params
      params.require(:ad).permit(:title, :url, images_attributes: [:preview])
    end
end

Ad _form View

<style type="text/css">
.header {
  display: none!important;
}
</style>

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

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

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <span>Contact (URL, Mobile, Address)</span><br>
    <%= f.text_field :url %>
  </div>
  <div class="field">
    <%= f.label :image %><br>
    <%= f.file_field :preview %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Ad Model

class Ad < ActiveRecord::Base

    attr_accessible :title, :url, :preview
    belongs_to :user
  has_attached_file :preview, :default_url => "missing.jpg"

  validates :title, length: { maximum: 20 }
  validates :url, length: { maximum: 20 }
end

Ad Index View

<h1>Listing ads</h1>




    <% @ads.each do |ad| %>
    <div class="adspace_grid">
      <%= image_tag ad.preview.url %>
      <div class="text_info">
      <span class="bold"><%= ad.title %></span> / 
      <span class="bold">Contact : </span><%= ad.url %>
      </div>
    </div> 
    <%= link_to 'Delete', ad, :method => :delete %> 
    <% end %>


<br>

<%= link_to 'New Ad', new_ad_path %>

Any help would be greatly appreciated. Thanks

EDIT : This is the error in the Google Chrome Console. Error Code

Upvotes: 0

Views: 395

Answers (1)

vee
vee

Reputation: 38645

The problem is where you are permitting ad attributes:

# Never trust parameters from the scary internet, only allow the white list through.
def ad_params
  params.require(:ad).permit(:title, :url, images_attributes: [:preview])
end

I'm not sure why you have images_attributes: [:preview] here. The method definition should just be:

def ad_params
  params.require(:ad).permit(:title, :url, :preview)
end

The reason you are not getting the uploaded picture and are getting the default missing.jpg is because the your image file selected in the form did not get created because you were not permitting :preview in your ad_params method.

Update:

As far as the path is concerned, you should be able to supply other parameters uch as url and path to has_attached_file. Something like following:

# app/models/ad.rb

has_attached_file :preview, :default_url => "missing.jpg", 
                  :url  => "/assets/ads/preview/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/preview/:id/:style/:basename.:extension"

With this all your uploaded preview files will be in public/assets/ads/preview/:id/:style/ directory.

Upvotes: 1

Related Questions