//安装插件:
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/
Paperclip: Attaching Files in Rails
最新推荐文章于 2024-05-09 09:42:27 发布