Notebook is a Ruby file attachment library, that has easy integration with Rails and ActiveRecord.
Add this line to your application's Gemfile:
gem 'notebook'
And then execute:
$ bundle
Or install it yourself as:
$ gem install notebook
Rails 4
class User < ActiveRecord::Base
notebook_attachment :profile_picture
end
<%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
Rails 4
def create
@user = User.create(user_params)
end
private
def user_params
params.require(:user).permit(:avatar)
end
<%= image_tag @user.avatar.url %>
Set the attribute to nil
and save.
@user.avatar = nil
@user.save
To use a different storage location than /public, create a
config/initializers/notebook.rb
, and then set the following:
Notebook.public_directory = 'hi'
To store your upload files in a different place than the filesystem,
create a config/initializers/notebook.rb
, then set the following:
# these are the two out of the box storage adapters
# to use one from another source, just place the constant of a
compliant adapter here.
Notebook.adapter = Notebook::StorageAdapters::Filesystem
Notebook.adapter = Notebook::StorageAdapters::S3
It's awesome you want to use plain Ruby without Rails with Notebook!
First off, you need to read in a file, and create a new
Notebook::Attachment
object, like so:
file = File.read("avatar.jpg")
attachment = Notebook::Attachment.new(file)
The last and final step is to just upload it!
attachment.upload
# get the url of the newly persisted file
attachment.url
Bug reports and pull requests are welcome on GitHub at https://github.com/moss-rb/notebook. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.