Upload paperclip images to s3 bucket - ruby

Not quite sure whats going on here, but when i try and upload an image to my s3 bucket i get this error
NameError in PostsController#create
uninitialized constant AWS::Core::ClientLogging
Rails.root: /home/richardlewis/Rails/myblog
Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:41:in `create'
Im testing this in my dev environment at present. This is my current setup
Gemfile
#Paperclip and aws
gem "paperclip", "~> 3.0"
gem 'aws-sdk'
gem 'aws-s3'
Image Model
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
attr_accessible :photo
has_attached_file :photo, :styles => { :small_blog => "250x250#", :large_blog => "680x224#", :thumb => "95x95#" },
:storage => :s3,
:url => ":s3_domain_url",
:s3_protocol => 'http',
:path => "/images/:id/:style.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
end
My ENV variables are stored in a env.rb file and loaded within initializers
has anyone come across this before?

Upgrading to the latest paperclip, 3.5.1, will fix this issue.

Related

Using model attribute as :filename when using paperclip gem

I'm getting an error when attempting to change the :filename of my paperclip attachment to equal an attribute on the class I'm attaching the paperclip file to.
When I use "#{self.company_name}" it errors out. Apparently in this scope, "self" is not Company. When I wrote this line I assumed that self is the instance of Company that I'm uploading this attachment to. Any idea how I can fix this? The Paperclip docs say to use ":filename" but I'd like to use the value of Company.company_name instead.
class Company < ActiveRecord::Base
include AliasAttrs
has_attached_file :company_logo, {
:storage => :ftp,
:path => "/logos/#{self.company_name}",
:url => FTP_CONFIG[:access_host]+"logos/:filename",
:ftp_servers => [
{
:host => FTP_CONFIG[:host],
:user => FTP_CONFIG[:user],
:password => FTP_CONFIG[:pass],
:port => 21 # optional, 21 by default
}
]
}
end
Update
I tried using the advice found in this post: https://robots.thoughtbot.com/paperclip-tips-and-updates
But now I am getting the following error when starting my server:
undefined method `interpolations' for Paperclip::Attachment:Class (NoMethodError)
It looks like the syntax for interpolations has changed. Updated it and it worked. Add the following to your model or create a paperclip.rb file in config/initializers
Paperclip.interpolates :company_name do |attachment, style|
attachment.instance.company_name
end

Add S3 with Carrierwave to Rails App for Heroku

I'm trying to add S3 to my Heroku app however, I'm getting the same problem I had on a previous question (carrierwave image not loading into source code) where the image url isn't loading into the source code.
Feature_image_uploader.rb has this instead of storage :file:
storage :fog
Gemfile:
gem 'carrierwave'
gem 'fog', '~> 1.3.1'
fog.rb file:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'xxx',
:aws_secret_access_key => 'yyy',
:region => 'eu-west-1',
:host => 's3.example.com',
:endpoint => 'https://s3.example.com:8080'
config.fog_directory = 'luchiauploads'
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
It has my access key ID and secret access key inserted. I also ran the terminal commands as per Heroku's instructions.
And this pastebin is my server log: http://pastebin.com/TH68bhn4
And rake tests have no errors.
I know I'm missing something really simple, but can't work out what. Thank you.
Error (line5 in pastebin):
Unpermitted parameters: feature_image_cache, remove_feature_image
you need to add these parameters to permited attributes:
portofolios_controller.rb
...
private
def portofolio_params
params.require(:portofolio).permit(:title, :date, :content, :feature_image, :feature_image_cache, :remove_feature_image)
end

Paperclip "Gem" Rails 3.1 undefined method model file name

Using rails 3.1.1, Ruby 1.9.2,
Gems: gem 'haml', gem 'simple_form', gem 'aws-sdk',
gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git"
plugin: country_select: git://github.com/rails/country_select.git
Having an issue uploading/displaying images pushed to Amazon S3 through paperclip (GEM)
Error: undefined method `avatar_file_name' for #Player:0x00000102aff228
For the most part I was following the example on the git-hub page for paperclip
https://github.com/thoughtbot/paperclip
Here is what I have in my code:
Migration: 20111224044508_create_players.rb
class CreatePlayers < ActiveRecord::Migration
def change
create_table :players do |t|
t.string :first_name
t.boolean :first_name_public, :default => false
...
t.string :website
t.boolean :website_public, :default => false
t.has_attached_file :avatar
t.timestamps
end
end
end
Model: Player.rb:
class Player < ActiveRecord::Base
attr_accessible :first_name, ... :website
validates_presence_of :username, :email
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":class/:id/:style/:filename"
{Unrelated validations}
end
S3 file: s3.yml
development:
bucket: voh_development
access_key_id: *********************
secret_access_key: ********************
staging:
bucket: voh_staging
access_key_id: *********************
secret_access_key: ********************
production:
bucket: voh_production
access_key_id: *********************
secret_access_key: ********************
Controller: players_controller.rb
class PlayersController < ApplicationController
def create
#player = Player.create(params[:player])
if #player.save
redirect_to players_path, :notice => "Player Created";
else
render :action => 'new'
end
end
{basic restful}
end
Views:
Edit.html.haml + New.html.haml
= simple_form_for #player do |f|
= f.input :first_name
...
= f.input :website
= f..file_field :avatar
.input_div
= f.button :submit
index.html.haml
...
%td Avatar
%td First Name
...
%td Actions
- #players.each do |player|
%tr
%td
= image_tag #player.avatar.url(:thumb)
%td
= player.first_name
...
%td
= link_to ' Show ', player_path(player.id)
|
= link_to ' Edit ', edit_player_path(player.id)
show.html.haml
= image_tag #user.avatar.url
%br
= #player.first_name
...
Research:
I found a lot to do with the pluging and genration of the migration but it all seems old. Most of them suggest putting in the up down in the migration for the 4 attributes. However it seems that should have been replaced by the one line t.has_attached_file :avatar.
I have a rails 3.0 project and this worked. I am able to upload products and pull them back down. (had to play with the suggested image_tag #icon.avatar.url and turned it into %img{:src => URI.unescape(icon.icon.url)} but that a different question.)
TLDR: Fixed typo in index.html.haml from #player => player, Added :avatar to attr_accessible.
I woke up this morning and had a different error.
instead of: undefined method Avatar_file_name'
I got: undefined method avatar' for nil:NilClass
That error was caused buy a simple type in my code. I used an instance vairable instead of .each variable I should have been using (index.html.haml:9)
Now the app was not erring out but the file was still not uploading.
In the development log I found this. (I did not look here the first time I posted)
WARNING: Can't mass-assign protected attributes: avatar
I then went and added :Avatar to attr_accessible and everything started working.
Not sure if this is supposed to be required or not but I did see that they had updated S3 header to be a proc yesterday.
https://github.com/thoughtbot/paperclip/tree/master/test/storage
I am not going to close this out yet. There will be an edit because I am going to play with the version and quickly report my findings today. Then I will close this out.
Edit:
I tryed switch back to 2.4.5 and I am getting the error that made me switch to pulling master in the first place. When attempting to do a migration with t.has_attached_file :avatar it fails to migrate and gives the following error.
undefined method `has_attached_file' for #ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x00000105053600
I think I will stick with pulling from master.

ruby paperclip to s3 error

I'm trying to use paperclip with heroku and s3, but I have many tables that can be associated with photos, we'll use :review for example.
I'm trying to seperate the photo from the review and upload that seperately, but since I'm new to ruby, I think I'm failing miserably.
I have the 'aws-s3' gem installed and bundled.
This is the error I'm getting:
LoadError in ReviewsController#create
no such file to load -- aws/s3 (You may need to install the aws-s3 gem)
Rails.root: C:/www/devise
Application Trace | Framework Trace | Full Trace
app/controllers/reviews_controller.rb:56:in `new'
app/controllers/reviews_controller.rb:56:in `block in create'
app/controllers/reviews_controller.rb:54:in `create'
app/controllers/redirect_back.rb:23:in `store_location'
This error occurred while loading the following files:
aws/s3
photo Model:
class Photo < ActiveRecord::Base
belongs_to :user
belongs_to :shop
belongs_to :baristum
belongs_to :review
#paperclip
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "400x400>",
:original => "800x800" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:style/:id/:filename"
end
photo schema:
t.string "file_name"
t.string "content_type"
t.integer "file_size"
t.integer "user_id"
t.integer "barista_id"
t.integer "review_id"
t.integer "shop_id"
t.datetime "created_at"
t.datetime "updated_at"
review Controller:
def create
#add the current user to the review hash, from the session var.
params[:review][:user_id] = current_user.id
#move the photo to another var, so I can remove it from the review insert
#photoUpload = params[:review][:photo]
params[:review].delete("photo")
#review = Review.new(params[:review])
respond_to do |format|
if #review.save
#photo = Photo.new(:photo => #photoUpload, :review_id => #review.id)
#photo.save
format.html { redirect_to(#review, :notice => 'Review was successfully created.') }
format.xml { render :xml => #review, :status => :created, :location => #review }
else
#shopList = Shop.find(:all)
format.html { render :action => "new" }
format.xml { render :xml => #review.errors, :status => :unprocessable_entity }
end
end
end
gemfile
source 'http://rubygems.org'
gem 'pg'
gem 'rake', '~> 0.8.7'
gem 'rails', '3.0.5'
#gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'devise', :git => 'git://github.com/plataformatec/devise', :branch => 'master'
gem 'omniauth', '0.2.0'
gem 'paperclip'
#gem 'RMagick'
gem "simple_form", "~> 1.2.2"
gem 'twitter_oauth', '0.4.3'
gem "rest-client", "1.6.1", :require => "restclient"
gem "sluggable"
gem 'gmaps4rails'
gem 'exception_notification', :require => 'exception_notifier'
gem 'yaml_db'
#gem 'mysql'
gem 'aws-s3'
#gem 'carrierwave'
#gem 'fog' #amazon s3
#gem 'nokogiri'
group :development, :test do
gem 'rspec-rails'
gem 'fixjour'
end
When you include in your gem file the 'aws-s3' gem remember to add the require statement.
gem 'aws-s3', :require => 'aws/s3'
Current versions of Paperclip use the aws-sdk gem, rather than the aws-s3 gem.
Try running the latest version of that gem, combined with the latest version of Paperclip which supports your Rails stack (Paperclip 2.x for Rails 2.3, or Paperclip 3.x for Rails 3+).
Looks like you need to have the following fields in your photos schema.
t.string :file_file_name
t.string :file_content_type
t.integer :file_file_size
t.datetime :file_updated_at
running this will generate a migration for you to do just that
#this convention: rails generate paperclip [model] [attachmentname]
rails generate paperclip photo file
You have to have the table columns named following this convention for paperclip to pick them up: 'attachmentname'_file_name, 'attachmentname'_content_type, etc... Where you're calling your Photo model's has_attachment "file".

Rails paperclip plugin

I am a newbie. I am trying to upload an image through paperclip. The url and path code is working but the style option is not working. This is my code:
class User < ActiveRecord::Base
has_attached_file :image, :styles => { :small => "150x150>" },
:url => "/assets/users/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/users/:id/:style/:basename.:extension"
end
When I use the style it doesn't work. If I remove the style option it does work. Please help me out!
Not sure if this will work but try :style_:basename.:extension

Resources