我有一个iOS api链接到rails应用程序。我已经发送了一个iOS应用程序的图像,我试图将其存储为header_img,但是我得到的错误是它们不是公共目录。在我的heroku API中指定图像的好路径是什么?我应该在哪里把图像放在一个heroku rails应用程序中?
这里是Heroku的错误:
2014-04-04T21:51:47.179916+00:00 app[web.1]:
2014-04-04T21:51:47.179916+00:00 app[web.1]: Errno::EISDIR (Is a directory - public/header_img/):
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:98:in `initialize'
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:98:in `open'
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:98:in `upload_header_img'
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:44:in `create'
这里是我的组控制器:
class GroupsController < ApplicationController
# GET /groups
# GET /groups.json
before_filter :require_auth
def index
@privategroups = @user.groups
@publicgroups = @user.followees(Group)
@groups = (@privategroups + @publicgroups).sort_by(&:created_at).reverse
end
# GET /groups/1
# GET /groups/1.json
def show
@group = Group.find(params[:id])
@posts = @group.posts
end
# GET /groups/new
# GET /groups/new.json
def new
@group = Group.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @group }
end
end
# GET /groups/1/edit
def edit
@group = Group.find(params[:id])
end
# POST /groups
# POST /groups.json
def create
@group = Group.new(params[:group])
@group.owner_id = @user.id
header_img = @group.header_img || @group.decode_header_img_data
if header_img.present?
upload_header_img(header_img)
end
respond_to do |format|
if @group.save
@membership = @user.memberships.build(group_id: @group.id)
@membership.save
format.html { redirect_to @group, notice: 'Group was successfully created.' }
format.json { render json: @group, status: :created, location: @group }
else
format.html { render action: "new" }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
# PUT /groups/1
# PUT /groups/1.json
def update
@group = Group.find(params[:id])
respond_to do |format|
if @group.update_attributes(params[:group])
format.html { redirect_to @group, notice: 'Group was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
# DELETE /groups/1
# DELETE /groups/1.json
def destroy
@group = Group.find(params[:id])
@group.destroy
respond_to do |format|
format.html { redirect_to groups_url }
format.json { head :no_content }
end
end
private
def upload_header_img(header_img_io)
# upload to the exact location account id and extension
extension = File.extname(header_img_io.original_filename)
# rails take public as root directory
# under the root directory (public)
# there are a icon directory
header_img_url = "/header_img/"[email protected]_s + extension
# open in binary mode
File.open("public"+header_img_url,'wb') do |file|
file.write(header_img_io.read)
end
@group.update_attribute(:header_img_url, header_img_url)
end
end
您可以创建在“/ tmp目录”的文件夹,但请记住图像将请求后销毁做完了。如果您想存储图片,则需要上传到AWS或其他存储站点。 – ChrisBarthol