Not implied character to string conversion, paper clips + rails 4
I am currently working on a proposal for rails 4 offering various subscription packages. I would like to upload images and have them in every package. I am using Amazon S3 to store photos using the "aws-sdk" gem.
I know it has something to do with the photo symbol: a hash, not a string, but I'm not sure what to do to resolve it.
When I submit my form, either nested from the package or on its own, I get the error
no implicit conversion of Symbol into String
Here are the relevant files. Thanks for the help.
config /development.rb
# Amazon S3 config for paperclip, required for Heroku, supplemented on development
config.paperclip_defaults = {
:storage => :s3,
:url => :s3_domain_url,
:s3_credentials => {
:bucket => 'oops',
:access_key_id => 'bigoops',
:secret_access_key => 'biggeroops'
}
}
models / package_photo.rb
class PackagePhoto < ActiveRecord::Base
belongs_to :package
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
end
models /package.rb
class Package < ActiveRecord::Base
default_scope { order('sort_order') }
has_many :package_photos
accepts_nested_attributes_for :package_photos, :reject_if => proc { |a| a[:photo].nil?}, :allow_destroy => true
end
Controllers / packages _controller.rb
class PackagesController < ApplicationController
before_action :set_package, only: [:show, :edit, :update, :destroy]
# GET /packages
# GET /packages.json
def index
@packages = Package.all()
end
# GET /packages/1
# GET /packages/1.json
def show
end
# GET /packages/new
def new
@package = Package.new
end
# GET /packages/1/edit
def edit
@package.package_photos.build
end
# POST /packages
# POST /packages.json
def create
@package = Package.new(package_params)
respond_to do |format|
if @package.save
format.html { redirect_to @package, notice: 'Package was successfully created.' }
format.json { render :show, status: :created, location: @package }
else
format.html { render :new }
format.json { render json: @package.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /packages/1
# PATCH/PUT /packages/1.json
def update
respond_to do |format|
if @package.update(package_params)
format.html { redirect_to packages_url, notice: 'Package was successfully updated.' }
format.json { render :show, status: :ok, location: @package }
else
format.html { render :edit }
format.json { render json: @package.errors, status: :unprocessable_entity }
end
end
end
# DELETE /packages/1
# DELETE /packages/1.json
def destroy
@package.destroy
respond_to do |format|
format.html { redirect_to packages_url, notice: 'Package was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_package
@package = Package.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def package_params
params.require(:package).permit(:title, :price, :description, :package_photos_attributes => [:id, :package_id, :sort_order, :photo, :_destroy])
end
end
view / packages / edit.html.erb
<%= package_form.fields_for :package_photos do |package_photo| %>
<div class="media image-form-control">
<div class="media-body">
<div class="btn btn-primary btn-sm btn-file">
Upload <%= package_photo.file_field :photo, :class => 'form-control'%>
</div>
<button class="btn btn-danger btn-sm package-image-delete" type="button">Delete</button>
<%= package_photo.check_box '_destroy', :class => 'hidden' %>
</div>
</div>
<% end %>
source to share
According to the paperclip documentation , for the option url
:
The value of this option is a string, not a character. right : ": s3_domain_url" is invalid :: s3_domain_url
In your code, the parameter url
is listed as Symbol
ie, :s3_domain_url
which is wrong and therefore results in below error
no implicit conversion of Symbol into String
url
the parameter value must be specified as String
ie, ":s3_domain_url"
( Notice ).
Update paperclip defaults
as shown below:
config.paperclip_defaults = {
:storage => :s3,
:url => ":s3_domain_url", ## Notice quotes "" around :s3_domain_url
:path => "/:class/:attachment/:id/:style/:filename", ## Add path option, specify the path as per your requirement
:s3_credentials => {
:bucket => 'oops',
:access_key_id => 'bigoops',
:secret_access_key => 'biggeroops'
}
}
Note:
When specifying an option :url
for, paperclip
you must also specify an option :path
.
source to share