1

with great difficulty , I guess I have finally been able to find the appropriate git commit of paperclip which would suit my project's config which is Ruby 1.8.7 and rails 2.0.2.

I am trying to implement a POC before I integrate paperclip in my project. The POC is basically implementing each post with an appropriate image beside it. It seems to pretty much work, its just that It currently showing routing errors when it is trying to fetch the image. I am able to see appropriate reflections in the DB showing that the image has been inserted with the post message. I actually in my Rails app can see the images being stored.. as per the default url used by paperclip to store the images in the file system.

But I am unable to understand why is it unable to use the same url to fetch back the image and display the same. I have basically implemented this POC using rails scaffold and made some appropriate changes.

The routing error talks about using the get method, I am unable to understand what role does get method play in fetching the image, I don't have any such method in my controller.. Something seems to be fishy wrt this scenario.. please enlighten me on the same on how exactly is the image supposed to be fetched and displayed.. if I am doing it wrong some where.. kindly correct me on the same..

I would like to add... a photos directory is created in my app's root directory and within that there is a directory which gets created every time a new post is added with an appropriate image upload. This directory name is coined with the an id, uniquely identifying each image within it. The actual directory structure is like this:-

rail_app_directory/photo/id_number/orginal/photo_filename.appropriate_file_format

A more direct e.g. to the general structure posted above:- post_with_image/photo/1/original/sunset.jpeg

Please find code for controller below just in case:-

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.xml
  def index
    @posts = Post.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.xml
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.xml
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.xml
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        flash[:notice] = 'Post was successfully created.'
        format.html { redirect_to(@post) }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.xml
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        flash[:notice] = 'Post was successfully updated.'
        format.html { redirect_to(@post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.xml
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end

Please find the code for my index.html.erb file:-

<h1>Listing posts</h1>

<table>
  <tr>
    <th>Message</th>
  </tr><br/>
</table>
<% for post in @posts %>
  <%= image_tag post.photo.url %>
  <table>
    <tr><td><%=h post.message %></td></tr>
    <tr><td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td></tr>
    </table>
  <br/>  <hr/>
<% end %>



<br />

<%= link_to 'New post', new_post_path %>

This is what I get doing a tail -f log/development.log

ActionController::RoutingError (No route matches "/photo/4/original/ruby-on-rails.jpg" with {:method=>:get}):
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1441:in `recognize_path'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1424:in `recognize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:170:in `handle_request'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:76:in `process'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `synchronize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `process'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `each'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/mongrel_rails:281
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/rails-2.0.2/lib/commands/servers/mongrel.rb:64
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/rails-2.0.2/lib/commands/server.rb:39
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

Rendering /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/templates/rescues/layout.erb (not_found)


Processing ApplicationController#index (for 127.0.0.1 at 2011-05-19 19:12:01) [GET]
  Session ID: BAh7ByIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%0ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlZWVhYzk4NzczZDNjZDI4ZmY0%0ANzQ0NTU0NGU2YWJkMjk%3D--b8294cd9465ee6ac96f69b34362e3044138f3ee2
  Parameters: {}


ActionController::RoutingError (No route matches "/photo/5/original/Water%20lilies.jpg" with {:method=>:get}):
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1441:in `recognize_path'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1424:in `recognize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:170:in `handle_request'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:76:in `process'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `synchronize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `process'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `each'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in `run'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/mongrel_rails:281
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/rails-2.0.2/lib/commands/servers/mongrel.rb:64
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require'
    /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/rails-2.0.2/lib/commands/server.rb:39
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

Rendering /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/templates/rescues/layout.erb (not_found)
^C** INT signal received.
Exiting
mohnish@mohnish-desktop:~/UP/pocs_tried_for_use/post_with_image$

[QUESTION EDITED]

Please find the code for the model post.rb:-

class Post < ActiveRecord::Base
  #require 'paperclip'
  has_attached_file :photo
end

My routes.rb files looks like this:-

ActionController::Routing::Routes.draw do |map|
  map.resources :posts

  # The priority is based upon order of creation: first created -> highest priority.

  # Sample of regular route:
  #   map.connect 'products/:id', :controller => 'catalog', :action => 'view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   map.resources :products

  # Sample resource route with options:
  #   map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }

  # Sample resource route with sub-resources:
  #   map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller

  # Sample resource route within a namespace:
  #   map.namespace :admin do |admin|
  #     # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
  #     admin.resources :products
  #   end

  # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
  # map.root :controller => "welcome"
  map.root :controller => "posts"

  # See how all your routes lay out with "rake routes"

  # Install the default routes as the lowest priority.
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

I am not sure where I am going wrong, Kindly help me with the same..

Thanks a lot for your help...

boddhisattva
  • 6,908
  • 11
  • 48
  • 72
  • Can you post your routes file and your model code showing the paperclip code? – Dylan Markow May 19 '11 at 14:35
  • Also you could use `<% @posts.each do |post| %> <%= image_tag post.photo.url %>` instead of the for in syntax – Caley Woods May 19 '11 at 15:17
  • @dmarkow: I have added the code for the model and the routes file to the question.. I would like to just mention, I was initially unsure that the my app would detect the gem or not.. hence I had originally given a require 'paperclip' , but I commented the same when I had verified that the app is successfully able to detect the plugin from the vendors directory of my rails app.. – boddhisattva May 19 '11 at 15:20
  • 1
    And in your `public/` folder is there a `photos/` folder? – Dylan Markow May 19 '11 at 15:25
  • @dmarkow: The photos folder is in my app's root or main directory.. It isn't within the public folder.. Should it be actually there..? – boddhisattva May 19 '11 at 15:33
  • @Caley: How would a different syntax make a difference..? I am sorry kindly correct me where I am going wrong..if your trying to show/point out at something I am missing... – boddhisattva May 19 '11 at 15:56
  • @dmarkow: Someone through another forum suggested me to place the photo folder under under the public directory..It worked.. no more routing errors.. But I don't get one thing.. why was the photo directory placed under the rails root directory in the 1st case.. I am exactly not sure which command created the photo directory.. probably if I cud tweak a part of the plugin somewhere 2 ensure tht it is placed under the public directory..this wouldn't happen in the first place.. but I am not sure where to tweak.. wht.. will hav to figure it out..:), yep but in the mean time i will use :url,:path.. – boddhisattva May 20 '11 at 03:43
  • @dmarkow:- I thought..it would be worth a try.. since even you suggested the same to me.. indirectly.... Thanks..:) – boddhisattva May 20 '11 at 03:46

1 Answers1

0

Just to formalize the answer from the comments on the question:

You need to make sure /photo/4/original/ruby-on-rails.jpg exists under your public folder, as that's where Paperclip should be storing all of its attachments by default.

Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • I would like to add.. This applies.. provided your using one of its latest commit...:) , I had used a commit of March 18 2008 to suit my config needs..from Github .. It had stored it for me in my rails root directory.. only after I copied into public it did work for me..Something which alway one can't keep doing for every new pic added..(This was my experience when I used the commit.., I am just mentioning this for the benefit of any1 who would need it.. not to contradict your answer or offend you in any way..) – boddhisattva May 21 '11 at 08:30