使用paperclip gem

时间:2016-08-12 15:34:59

标签: ruby-on-rails ruby

我正在尝试使用paperclip gem来显示图像,但输出却让它丢失了图像

下面分别是控制器和模型文件

CONTROLFILE

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 / middleware / debug_exceptions.rb:21:在call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in
块中   调用'web-console(2.3.0)lib / web_console / middleware.rb:18:in   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:in   tagged' activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in标记为'railties(4.2.6)   lib / rails / rack / logger.rb:20:in call' actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in call'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 /缓存/策略/ 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.6)   lib / rails / engine.rb:518:在call' railties (4.2.6) lib/rails/application.rb:165:in呼叫'机架(1.6.4)   lib / rack / lock.rb:17:in call' rack (1.6.4) lib/rack/content_length.rb:15:in call'rack(1.6.4)   lib / rack / handler / webrick.rb:88:in service'
C:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:138:in
service'
  C:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:94:in run'
C:/Ruby21-x64/lib/ruby/2.1.0/webrick/server.rb:295:in
块中   start_thread'

     

渲染   C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(3.0ms)呈现   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)呈现   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)呈现   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)呈现   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:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb   在layouts / inlined_string(1.0ms)内呈现   C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb   在layouts / inlined_string(1.0ms)内呈现   C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb   在layouts / inlined_string(1.0ms)内呈现   C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb   在布局/ javascript(288.3ms)内呈现   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)内呈现   C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb   (431.4ms)

1 个答案:

答案 0 :(得分:0)

在你的erb视图中你应该有类似

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

取决于您要显示的尺寸。