我目前正在开发rails 4 app提供不同的订阅套餐。我想上传图片并让它们属于每个包。我使用Amazon S3使用'aws-sdk'宝石存储照片。
我知道这与以下内容有关:照片符号是哈希而不是字符串,但我不知道如何解决它。
当我提交我的表单时,无论是从包中嵌套,还是单独提交,我都会收到错误
no implicit conversion of Symbol into String
以下是相关文件。谢谢你的帮助。
配置/ 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'
}
}
模型/ 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
模型/ 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
控制器/ 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
视图/包/ 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 %>
答案 0 :(得分:3)
根据Paperclip documentation,url
选项:
此选项的值是字符串,而不是符号。的右强>: “:s3_domain_url”错误 :: s3_domain_url
在您的代码中,url
选项被指定为Symbol
,即:s3_domain_url
不正确,因此导致以下错误
no implicit conversion of Symbol into String
url
选项值应指定为String
,即":s3_domain_url"
(通知周围的引号)
更新paperclip defaults
,如下所示:
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'
}
}
注意:强>
为:url
指定paperclip
选项时,您还应指定:path
选项。