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".
Related
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
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.
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.
How to do that I have got resource in sinatra
http://localhost:port/resource.json ??
Install the json gem:
sudo gem install json
then just
require 'json'
get '/resource.json' do
content_type :json
{ :name => 'Michal', :location => 'unknown' }.to_json
end
I'm trying to use ActionMailer without Rails in a project, and I want to use Haml for the HTML email templates. Anyone have any luck getting this configured and initialized so that the templates will be found and rendered? I'm currently getting errors like:
ActionView::MissingTemplate: Missing template new_reg/daily_stats/full with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en]} in view paths "/home/petersen/new_reg/lib/new_reg/mailers/views"
To clarify, this is ActionMailer 3.0.4
Looks like the issue is that without the full Rails stack, Haml doesn't completely load, specifically the Haml::Plugin class. Adding require 'haml/template/plugin' after the normal require 'haml' line seems to solve the problems.
require 'haml/template/plugin' in the "configure do" block together with ActionMailer::Base.view_paths = "./views/" did it for me (Sinatra)
Not necessary in Rails -- but since you're using ActionMailer without Rails -- did you specify ActionMailer::Base.register_template_extension('haml')?
I'm seeing a similar issue and am using ActionMailer 3.0.3. register_template_extension does not exist in ActionMailer 3.
I'm using Sinatra. I've got mailer.rb (below) in APP_ROOT/lib and the views are located in APP_ROOT/views/mailer. This sends an email with a subject, the body is blank though.
require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.view_paths = File.dirname(__FILE__)+"/../views/"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'exmaple.com',
:user_name => 'user#exmaple.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true }
class Mailer < ActionMailer::Base
def new_comment_notifier(post,comment)
#post = post
#comment = comment
mail(:to => "user#example.com",
:subject => "new comment on: #{post.title}")
end
end