无法通过文件系统使用回形针插件通过get方法

问题描述:

很难找到图像路径,我想我终于能够找到适合我的项目配置的Ruby 1.8的适当的git提交。 7和导轨2.0.2。无法通过文件系统使用回形针插件通过get方法

我试图在将回形针集成到我的项目中之前实施POC。 POC基本上在每个帖子旁边都贴上了一个适当的图片。它似乎很多工作,它只是它目前显示路由错误当它试图获取图像。我能够在数据库中看到适当的反射,显示图像已经插入了发布信息。实际上,在我的Rails应用程序中,可以看到正在存储的图像。按照回形针用于将图像存储在文件系统中的默认网址。

但我无法理解为什么它无法使用相同的URL来取回图像并显示相同。我已经基本实施了这个POC使用轨道脚手架,并做了一些适当的修改。

路由错误谈到使用get方法,我无法理解在获取图像时获取方法的角色是什么,在我的控制器中我没有任何这样的方法.. 东西似乎是鱼腥味这个场景..请赐教我关于如何是应该被提取和显示的图像..如果我做错了一些地方..请在相同的地方纠正我..

我想添加...照片目录是在我的应用程序的根目录中创建的,其中有一个目录,每次添加新帖子并添加适当的图片时都会创建一个目录。该目录名称是用一个ID创建的,唯一标识其中的每个图像。实际的目录结构是这样的: -

rail_app_directory/photo/id_number/orginal/photo_filename.appropriate_file_format 

更直接的例如上面贴的一般结构: - post_with_image/photo/1/original/sunset.jpeg

请在下面找到以防万一的控制器代码: -

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 

请找到代码为我的index.html.erb文件: -

<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 %> 

这是我得到做尾-f日志/ 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 
[email protected]:~/UP/pocs_tried_for_use/post_with_image$ 

[问题EDITED]

请找到该模型post.rb代码: -

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

我的routes.rb文件看起来是这样的: -

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 

我我不知道我要去哪里错,请帮助我一样..

非常感谢您的帮助...

+0

你可以发布你的路线文件和显示回形针代码的模型代码吗? – 2011-05-19 14:35:58

+0

你也可以用' '而不是for in语法 – 2011-05-19 15:17:59

+0

@dmarkow:我已经为模型和路径文件添加了代码以用于问题..我只想提到,我最初我不确定我的应用程序是否会检测到宝石。因此,我最初给出了一个require'paperclip',但是当我确认应用程序能够成功地从供应商目录中检测插件时,我评论了相同的内容我的导轨应用程序.. – boddhisattva 2011-05-19 15:20:07

只是为了对这个问题的意见正式的答案:

你需要确保你的/photo/4/original/ruby-on-rails.jpgpublic文件夹下存在的,因为这就是回形针应该是默认存储所有的附件。

+0

我想补充..这适用..提供您使用它的一个lates t commit ... :),我用2008年3月18日的一个提交来满足我的配置需求..从Github ..它已经将它存储在我的rails根目录中..只有在我复制到公共目录后才能工作对于我来说......总是有人无法继续为每一张新图片添加..(这是我使用提交时的经验..,我只是提到这是为了任何人的利益,谁会需要它..不是反驳你的答案或以任何方式冒犯你......) – boddhisattva 2011-05-21 08:30:33