2016-08-12 63 views
-1

我試圖使用回形針寶石來顯示圖像,但輸出的時候,我得到它缺少像缺少圖片使用回形針寶石

下面是控制器和模型分別

文件控制文件

class HandymenController < ApplicationController 
    before_action :find_handyman, only: [:show, :edit, :update, :destroy] 

    def index 
     if params[:profession].blank? 
      @handymen = Handyman.all.order("created_at DESC") 
     else 
      @profession_id = Profession.find_by(name: params[:profession]).id 
      @handymen = Handyman.where(:profession_id => @profession_id).order("created_at DESC") 
     end 
    end 

    def show 
    end 

    def new 
     @handyman = current_user.handymen.build 
     @professions = Profession.all.map{ |p| [p.name, p.id] } 
    end 

    def create 
     @handyman = current_user.handymen.build(handyman_params) 
     @handyman.profession_id = params[:profession_id] 

     if @handyman.save 
      redirect_to root_path 
     else 
      render 'new' 
     end 
    end 

    def edit 
     @professions = Profession.all.map{ |p| [p.name, p.id] } 
    end 

    def update 
     @handyman.profession_id = params[:profession_id] 
     if @handyman.update(handyman_params) 
      redirect_to handyman_path(@handyman) 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @handyman.destroy 
     redirect_to root_path 
    end 

    private 

    def handyman_params 
     params.require(:handyman).permit(:name, :location, :phone_number, :profession_id, :handyman_img) 
    end 

    def find_handyman 
     @handyman = Handyman.find(params[:id]) 
    end 
end 

模型文件

class Handyman < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :profession 

    has_attached_file :handyman_img, styles: { handyman_index: "250x350>", handyman_show: "325x475>" } 
    validates_attachment_content_type :handyman_img, content_type: /\Aimage\/.*\Z/ 
end 

錯誤日誌

的ActionController :: RoutingError(沒有路由匹配[GET] 「/handyman_imgs/handyman_show/missing.png」):ActionPack的(4.2.6) LIB/action_dispatch /中間件/ debug_exceptions.rb:21:在call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in
block 調用'web-console(2.3.0)lib/web_console/middleware.rb:18:在 catch' web-console (2.3.0) lib/web_console/middleware.rb:18:in 調用'actionpack(4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in
call_app'railties (4.2.6)lib/rails/rack/logger.rb:20:在block in call' activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in中標記爲'
activesupport(4.2.6)lib/active_support/tagged_logging.rb:26:在(1.2.6) lib/rails/rack/logger.rb:20:在call' actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in中調用'rack (1.6.4)lib/rack/methodoverride.rb:22:在call' rack (1.6.4) lib/rack/runtime.rb:18:in中調用'activesupport( 4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:在 call' rack (1.6.4) lib/rack/lock.rb:17:in調用'actionpack(4.2.6)lib/action_dispatch/middleware/static.rb:120:在call' rack (1.6.4) lib/rack/sendfile.rb:113:in調用'railties(4.2 (1.6.4) lib/rack/lock.rb:17:在call' rack (1.6.4) lib/rack/content_length.rb:15:in調用'rack(1.6.4) lib/rack/engine.rb:518:in call' railties (4.2.6) lib/rails/application.rb:165:in調用'rack(1.6.4) lib/rack /處理程序/ webrick.rb:88:在service'
C:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:138:in
服務'
C :/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:94:在 start_thread」

渲染 Çrun'
C:/Ruby21-x64/lib/ruby/2.1.0/webrick/server.rb:295:in
塊:/Ruby21-x64/lib/ruby/gems/2.1。 0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(3.0ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/ actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb(4.0ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/actionpack-4.2。 6/lib/action_dispatch/middleware/templates/routes/_table.html.erb(120.1ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/ action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.0ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb 內部救援/佈局(409.4ms)渲染 C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (1.0ms的)呈現 C:/在layouts/inlined_string(1.0ms)內呈現的Ruby21-x64/lib/ruby​​/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb Rendering C:/ Ruby21 -x64/lib/ruby​​/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string(1.0ms)Rendered C:/ Ruby21- X64/lib中/紅寶石/寶石/ 2.1.0 /寶石/ Web的控制檯-2.3.0/lib目錄/ web_console /模板/ style.css中。erb in layouts/inlined_string(1.0ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb layouts/javascript(288.3ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb layouts/javascript(1.0ms)內渲染 C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb佈局內/ javascript(1.0ms)Rendered C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (431.4ms )

+0

如何在視圖中顯示圖像?錯誤日誌在哪裏? ... – kirqe

+0

只需一秒鐘 – Phraseassembler

+0

這裏是show.html.erb文件 – Phraseassembler

回答

0

在您的ERB查看您應該有類似

<%= image_tag(@handyman.handyman_img(:handyman_index)) %> 
<%= image_tag(@handyman.handyman_img(:handyman_show)) %> 

這取決於你想展示一下尺寸。

+0

哪個erb文件你在說什麼? – Phraseassembler

+0

他意味着你想要顯示圖像的視圖文件。 –

+0

我在show.html.erb文件中做了它,它沒有改變它 – Phraseassembler