Paperclip app using Ruby on Rails
kiran@kiran:~/Desktop/today$
cd paperclip
kiran@kiran:~/Desktop/today/paperclip$
|
kiran@kiran:~/Desktop/today/paperclip$ rails g scaffold Paper name:string email:string |
kiran@kiran:~/Desktop/today/paperclip$ rake db:migrate |
kiran@kiran:~/Desktop/today/paperclip$ rails s |
Now Open you Gemfile and add... gem 'paperclip', '~> 4.3.0' |
bundle install |
Note: After that you have to migrate the rails model with your image. kiran@kiran:~/Desktop/today/paperclip$ rails g migration add_image_to_papers |
Now open your
db/migration/20150618055156_add_image_to_papers.rb
and
add the follwing
class
AddImageToPapers < ActiveRecord::Migration
def
self.up
add_column
:papers, :image_file_name, :string
add_column
:papers, :image_content_type, :string
add_column
:papers, :image_file_size, :integer
add_column
:papers, :image_updated_at, :datetime
end
def
self.down
remove_column
:papers, :image_file_name
remove_column
:papers, :image_content_type
remove_column
:papers, :image_file_size
add_column
:papers, :image_updated_at
end
end
|
After you will go your model
app/models/paper.rb class Paper < ActiveRecord::Base has_attached_file :image end |
Then after you will go and open
to change modifications app/views/papers/_form.html.erb
adding your form name in the 1st line of your form. This is used for mutliple images you can upload. <%= form_for(@paper, :html => {:multipart => true}) do |f| %>
and
then add field in same app/views/papers/_form.html.erb
<%= f.label :image %><br> <%= f.file_field :image %> </div> |
If you got any error like this "undefined method for `has_attached_file`”
then
restart your rails server.
|
Option 1: Validate content type
Now open your model: app/models/paper.rb
-OR-
another way
|
Note: This is tagged as
Rails 4, so I think the strong parameter whitelisting in your
controller should do fine. Take out the
attr_accessor : image _file_name and
it should work.If you are using Rails 4 below then use attr_accessors
in your model |
Comments
Post a Comment