`
xf986321
  • 浏览: 163891 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics