【问题标题】:Uploading Images using paperclip in rails 3在rails 3中使用回形针上传图像
【发布时间】:2012-07-03 13:07:36
【问题描述】:

我正在尝试在我的 Rails 应用程序中上传图像。但是,我无法这样做。我对 Rails 比较陌生,在这里需要一些帮助。

这里显示了一个“图片”模型 -

 class Picture < ActiveRecord::Base

     attr_accessible :photo, :tag

     has_attached_file :photo, :styles => { :medium => "300x300>",
     :large => "1000x1000>",     :thumb => "100x100>" }

 end

这里显示了一个“图片”控制器 -

 class PicturesController < ApplicationController

    def new
            @picture = Picture.new
    end

    def create
            @picture = Picture.create( params[:picture] )
            if @picture.save
                    flash.now[:success] = "Image Uploaded"
                    redirect_to @picture
            else
                    render 'new'
            end
    end

    def index
    end

    def show
            @picture = Picture.find(params[:id])
            send_data @picture.photo, :type => 'image/png', :disposition => 'inline'
    end

    def imageshow
    end

 end

最后,这些是我的“新”和“展示”观点:

“new.html.erb”

 <h1>Upload an Image</h1>
 <div>
    <%= form_for(@picture, :html => {:multipart => true}) do |f| %>
       <%= f.file_field :photo %>
       <%= f.label :tag %>
       <%= f.text_field :tag %>
       <%= f.submit "Submit", class: "btn btn-large btn-primary" %>
    <% end %>
 </div>

“show.html.erb”

 <h1>Displaying Uploaded Image</h1>
 <div>
     <%= image_tag @picture.photo.url %>
     <%= image_tag @picture.photo.url(:large) %>
 </div>

我能够访问上传页面(即图片控制器中的 new.html.erb)。但是,当我上传图片时,出现以下错误:

“此图片“http://10.102.119.20:3000/pictures/12”无法显示,因为它包含错误”

我的查询是:

  1. 图片从哪里上传到服务器?
  2. 有什么需要做的配置吗?
  3. 我的代码还有其他问题吗?

提前感谢您的帮助..!!

【问题讨论】:

  • 我知道,上次我使用回形针时,图像被上传到public目录
  • 雅斯基,这是真的。我不知何故发现默认情况下,图像会上传到 /public 目录。准确地说,/root/rails_project/sample_app/public/system/pictures/photos.. 但是,我无法在图片控制器中使用 show 方法显示图像。您可以在上面的 show 方法中看到我正在使用的内容。有什么想法吗??

标签: ruby-on-rails-3


【解决方案1】:

Ryan Bates 讨论 Paperclip in his Railscasts。看看这个。它帮助我入门。

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多