2011-05-19 62 views
1

很難找到圖像路徑,我想我終於能夠找到適合我的項目配置的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

你也可以用'<%@ posts.each do | post | %> <%= image_tag post.photo.url%>'而不是for in語法 – 2011-05-19 15:17:59

+0

@dmarkow:我已經爲模型和路徑文件添加了代碼以用於問題..我只想提到,我最初我不確定我的應用程序是否會檢測到寶石。因此,我最初給出了一個require'paperclip',但是當我確認應用程序能夠成功地從供應商目錄中檢測插件時,我評論了相同的內容我的導軌應用程序.. – boddhisattva 2011-05-19 15:20:07

回答

0

只是爲了對這個問題的意見正式的答案:

你需要確保你的/photo/4/original/ruby-on-rails.jpgpublic文件夾下存在的,因爲這就是回形針應該是默認存儲所有的附件。

+0

我想補充..這適用..提供您使用它的一個lates t commit ... :),我用2008年3月18日的一個提交來滿足我的配置需求..從Github ..它已經將它存儲在我的rails根目錄中..只有在我複製到公共目錄後才能工作對於我來說......總是有人無法繼續爲每一張新圖片添加..(這是我使用提交時的經驗..,我只是提到這是爲了任何人的利益,誰會需要它..不是反駁你的答案或以任何方式冒犯你......) – boddhisattva 2011-05-21 08:30:33