raisl 上传图片的几种方法

方法一

使用 file-column 和rmagick 上传

安装插件 Ruby script/plugin install http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk

做法

.建立一个存放路径的model,在数据库中建立Entry数据库

并生成相应的scaffold:

ruby script/generate scaffold Entry upload

4.修改model,并限制只能图片上传

代码

class Entry < ActiveRecord::Base  

   validates_format_of :image,  

     :with=>/^.*(.jpg|.JPG|.gif|.GIF)$/,  

     :message => "你只能上传JPG或则GIF的图片文件" 

   file_column :image, :magick => {  

           :versions => { "thumb" => "50x50", "medium" => "640x480>" }  

         }  

end 

5.修改_form.rhtml

代码

<%= error_messages_for 'entry' %> 

<p><label for="entry_image">Imagelabel><br/> 

<%= file_column_field 'entry', 'image'   %>p> 

6.修改new.rhtml

代码

<h1>New entryh1> 

<%= start_form_tag 'create',:multipart => true%> 

  <%= render :partial => 'form' %> 

  <%= submit_tag "Create" %> 

<%= end_form_tag %> 

<%= link_to 'Back', :action => 'list' %> 

7.修改show.rhtml

代码

<% for column in Entry.content_columns %> 

<p> 

  <b><%= column.human_name %>:b> <%=h @entry.send(column.name) %> 

  <br> 

  原始大小:  

  <%= image_tag url_for_file_column 'entry', 'image' %> 

  <br> 

   thumb:  

  <%= image_tag url_for_file_column 'entry', 'image' ,'thumb'%> 

  <br> 

   medium:  

  <%= image_tag url_for_file_column 'entry', 'image' ,'medium'%> 

p> 

<% end %> 

<%= link_to 'Edit', :action => 'edit', :id => @entry %> |  

<%= link_to 'Back', :action => 'list' %> 

   1. 遇到问题

my_options = FileColumn::init_options(options, 

                                      Inflector.underscore(self.name).to_s, 

                                       attr.to_s) 

改成:

Ruby代码

my_options = FileColumn::init_options(options,  

                                       ActiveSupport::Inflector.underscore(self.name).to_s, 

                                       attr.to_s)

方法二 使用attachment_fu 和 minimagick 和 rmagick 上传图片

安装

gem install mini_magick 或者是 gem install rmagick(window下 要安装windows版本)

Controller 部分

def create

    @mugshot = Mugshot.new(params[:mugshot])

    respond_to do |format|

      if @mugshot.save

        flash[:notice] = 'Mugshot was successfully created.'

        format.html { redirect_to(@mugshot) }

        format.xml { render ml => @mugshot, :status => :created, :location => @mugshot }

      else

        format.html { render :action => "new" }

        format.xml { render ml => @mugshot.errors, :status => :unprocessable_entity }

      end

    end

end

Model

require 'mini_Magick'

class Mugshot < ActiveRecord::Base

has_attachment :content_type => :image,

    :storage => :file_system,

    :processor => :MiniMagick,

    :max_size => 10.megabytes,

    :resize_to => '320x200',

    :thumbnails => { :thumb => [50, 50] },

    :path_prefix => 'public/images/mugshots'

validates_as_attachment

end

视图

<h1>New mugshot</h1

<%= error_messages_for :mugshot %>

<% form_for(:mugshot, :url => mugshots_path,

                      :html => { :multipart => true }) do |f| -%>

<p>

    <label for="mugshot">Upload A Mugshot:</label>

    <%= f.file_field :uploaded_data %>

</p>

<p>

    <%= submit_tag 'Create' %>

</p>

<% end -%>

显示

<%= link_to 'New mugshot', new_mugshot_path %>

<h1>Most Wanted</h1>

<% for mugshot in @mugshots -%>

<%= link_to image_tag(mugshot.public_filename(:thumb)), mugshot.public_filename %>

<% end -%>

Imagemagick 安装

http://rubyforge.org/frs/?group_id=12&release_id=3031


最后 一个问题

no such file to load -- mini_Magick

no such file to load -- RMagick

在命令行下启动 服务器 和在netbeans 启动 都试一下

你可能感兴趣的:(xml,Ruby,Netbeans,Rails,ActiveRecord)