2016-08-12 31 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