Paperclip: Attaching Files in Rails

//安装插件:
script/plugin install git://github.com/thoughtbot/paperclip.git

script/generate migration add_columns-to_tasks

def self.up
    add_column :tasks, :photo_file_name,    :string
    add_column :tasks, :photo_content_type, :string
    add_column :tasks, :photo_file_size,    :integer
    add_column :tasks, :photo_updated_at,   :datetime
  end

  def self.down
    remove_column :tasks, :photo_file_name
    remove_column :tasks, :photo_content_type
    remove_column :tasks, :photo_file_size
    remove_column :tasks, :photo_updated_at
  end

rake db:migrate

Task model
  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }

//new页面
<h1>New task</h1>

<% form_for :task, :url => {:action => :create}, :html => { :multipart => true } do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :Photo %><br />
    <%= f.file_field :photo %>
  </p>
  <p>
    <%= f.label :position %><br />
    <%= f.text_field :position %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', tasks_path %>

controller:
 @task = Task.new(params[:task])
 if @task.save
 redirect_to @task
else
render :action => :new
end

  //show 页面
<p>
  <b>Position:</b>
  <%=h @task.position %>
  <%= image_tag @task.photo.url %>
  <%= image_tag @task.photo.url(:medium) %>
  <%= image_tag @task.photo.url(:thumb) %>
    
</p>
详细地址:http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/
http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值