Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我应该在heroku rails应用程序中将图像放在哪里?_Ruby On Rails_Heroku - Fatal编程技术网

Ruby on rails 我应该在heroku rails应用程序中将图像放在哪里?

Ruby on rails 我应该在heroku rails应用程序中将图像放在哪里?,ruby-on-rails,heroku,Ruby On Rails,Heroku,我有一个iOS api链接到rails应用程序。我已经向rails应用程序发送了一个iOS映像,我试图将其存储为header\u img,但收到一个错误,它们不是公共目录。在我的heroku api中指定图像的好路径是什么 以下是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 - pu

我有一个iOS api链接到rails应用程序。我已经向rails应用程序发送了一个iOS映像,我试图将其存储为header\u img,但收到一个错误,它们不是公共目录。在我的heroku api中指定图像的好路径是什么

以下是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/"+@group.id.to_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
类组控制器
虽然我不能提供直接的答案,但我有一些想法可能会有所帮助:


,它们运行只读文件系统:

你的应用程序被编译成slug,由dyno快速发布 经理slug的文件系统是只读的,这意味着 无法为半永久性存储动态写入文件系统。 不支持以下类型的行为:

  • 在公共目录中缓存页面
  • 将上载的资产保存到本地 磁盘(例如带有附件或回形针)
  • 编写全文索引 使用Ferret写入文件系统数据库,如SQLite或GDBM
  • 访问git wiki等应用程序的git回购
校正

从那以后,我发现上面是他们的
堆(旧)。委员会:

每个dyno都有自己的临时文件系统,其中包含 最近部署的代码。在dyno的生命周期中,它在运行 进程可以将文件系统用作临时草稿行,但不能 写入的文件对任何其他dyno和 在dyno停止或停止时,写入的任何文件都将被丢弃 重新启动

我们使用回形针并上传到Heroku(在测试中),它通常不受支持,每次重新部署应用程序时,任何文件都将被覆盖


存储

ChrisBarthol
给出了一个很好的建议-将用户提交的所有
资产存储在
S3
或其他第三方系统上。我想进一步说,您可能会从使用保存文件中受益匪浅:

#app/models/group.rb
Class Group < ActiveRecord::Base
    has_attached_file :header_img
       :storage => :s3,
       :s3_credentials => Proc.new{|a| a.instance.s3_credentials }

    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/   
end
#app/models/group.rb
类组:s3,
:s3_credentials=>Proc.new{| a | a.instance.s3_credentials}
验证\u附件\u内容\u类型:头像,:内容\u类型=>/\Aimage\/.\Z/
结束

您可以在“/tmp”下创建文件夹,但请记住,请求完成后图像将被销毁。如果要存储图像,需要将其上载到AWS或其他存储站点。