简体   繁体   中英

My image is uploading, but to nowhere it seems (it's not showing at all) - haven't a clue where I'm going wrong

Currently using PaperClip to upload an image file. In my form I am easily able to update/edit or create a new listing. When I upload an image it only shows up as a broken link and when I click 'open image in new tab' I get a message saying:

No route matches [GET] "/assets/images/missing.png

Am utterly stuck and have no idea whatsoever of what to do next. Would be truly grateful if someone could please suggest some pointers or tips on what to do?

EDIT:

This is my controller:

class ListingsController < ApplicationController
  before_action :set_listing, only: [:show, :edit, :update, :destroy]

  # GET /listings
  # GET /listings.json
  def index
    @listing = Listing.all
  end

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

  # GET /listings/new
  def new
    @listing = Listing.new
  end

  # GET /listings/1/edit
  def edit
  end

  # POST /listings
  # POST /listings.json
  def create
    @listing = Listing.create( listing_params )
    respond_to do |format|
      if @listing.save
        format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
        format.json { render action: 'show', status: :created, location: @listing }
      else
        format.html { render action: 'new' }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /listings/1
  # PATCH/PUT /listings/1.json
def update
    @listing = Listing.find(params[:id]) #find item you want to update
    respond_to do |format|
      if @listing.update_attributes(listing_params[:id]) # the refer to it and update it
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
    end
  end
end 



  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
    @listing.destroy
    respond_to do |format|
      format.html { redirect_to listings_url }
      format.json { head :no_content }
    end
  end

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

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

Here is my model - I just wanted to upload the file first, irrespective of size.

class Listing < ActiveRecord::Base
  has_attached_file :image, 
                    :styles => { :medium => "200x200", :thumb => "100x100>" }
end

Here is my development log:

Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100


Started GET "/assets/listings.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100


Started GET "/assets/pages.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100


Started GET "/images/original/missing.png" for 127.0.0.1 at 2014-06-09 00:23:48 +0100

ActionController::RoutingError (No route matches [GET] "/images/original/missing.png"):
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  C:/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  C:/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  C:/Ruby200/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-     4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.0ms)
  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout   (116.0ms)

HI @USERsomenumbers...

In the Documetation for Parperclip . They showed an example of how should look your Model (listing.rb)

class Listing < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "http://i.kinja-img.com/gawker-media/image/upload/s--iEV2_cUo--/c_fill,fl_progressive,g_north,h_77,q_80,w_137/akjmndys0n7jgpfmf41d.jpg"
end

The :default_url option stands for a replacement in case that a user didn't upload an image and if you didn't write it in your Model it will have a default url ( '/images/thumb/missing.png' ... or something similar) but you'll have an error cause there is no routes (link) in your app that has that file. So you should always write a default url OR add a Validation (:presence true) to force the user to upload an image

Also...to verify if your app did save your upload you should log in the console ( rails c -s ) and Listing.last

2.1.0 :001 > Listing.last
  Listing Load (0.3ms)  SELECT "listings".* FROM "listings" ORDER BY "listings"."id" DESC LIMIT 1
 => #<Listing id: 3, name: "34", description: "54", price: #<BigDecimal:93f0120,'0.77E2',9(27)>, created_at: "2014-06-09 06:02:20", updated_at: "2014-06-09 06:02:20", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil> 

As you see for all the symbol that starts with :image the value is nil so it means the user didn't upload an image and that rails will look for an image that is set in a :default_url

Also you should have this line on your _form.html.erb

<%= form_for @listing,  :url => listings_path, :html => { :multipart => true } do |f| %>

I am not sure if rails saved an image path

Rails / Paperclip will not save an image path - Paperclip will have a folder / file structure where it will save the files you need (this is defaulted to public/system )

Paperclip works by taking your file, converting it to an object & saving the following pieces of information to your database:

  • attachment_file_name
  • attachment_content_type
  • attachment_file_size
  • attachment_updated_at

This means your problem is not likely to be with a lack of image path from Paperclip. Paperclip is literally just a way to append an image object to your record, allowing you to do something like this:

@post = Post.find 1
@post.image.url

Validation

I think your problem is with your validation

Paperclip 4 introduced a new type of validation, which checks file spoofing before uploading (basically checks the file type). The way to fix this is to use the following validation:

#app/models/listing.rb
class Listing < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

Your other code looks good - I would suggest the problem is your file is not being validated correctly, leading it to not get saved. Either that, or you're calling the file incorrectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM