【问题标题】:Rails unable to upload file using paperclipRails 无法使用回形针上传文件
【发布时间】:2016-01-29 07:28:29
【问题描述】:

我在关注这个tutorial


我正在尝试使用回形针 gem 上传文件..当我尝试上传文件时出现错误 Method Not Allowed 这就是为什么我的资产表为空白没有数据插入

[资产.rb]

class Asset < ActiveRecord::Base

   belongs_to :user
    has_attached_file :uploaded_file

    validates_attachment_size :uploaded_file, :less_than => 10.megabytes   
    validates_attachment_presence :uploaded_file


  def file_name 
    uploaded_file_file_name 
end

def file_size 
    uploaded_file_file_size 
end
end


[用户.rb]

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

    validates :email, :presence => true, :uniqueness => true

    has_many :assets



end


[assets_controller]

class AssetsController < ApplicationController

       before_filter :authenticate_user!  #authenticate for users before any methods is called 


      def index 
        @assets = current_user.assets   

      end

      def show 
        @asset = current_user.assets.find(params[:id]) 
      end

      def new
        @asset = current_user.assets.new
      end

      def create 
        @asset = current_user.assets.new(user_assets) 
        if @asset.save
           flash[:notice] = "Successfully uploaded the file."
        else
           render :action => 'new'
        end

      end

      def edit 
        @asset = current_user.assets.find(params[:id]) 
      end

      def update 
        @asset = current_user.assets.find(params[:id]) 

      end

      def destroy 
        @asset = current_user.assets.find(params[:id]) 

      end


    private
        def user_assets
             params.require(:asset).permit(:user_id, :uploaded_file)
        end

    end
    <br>

[资产/_form.html.erb]

<%= form_for @asset, :html => {:multipart => true} do |f| %> 
  <%= f.error_messages %> 

    <p> 
        <%= f.label :uploaded_file, "File" %><br /> 
        <%= f.file_field :uploaded_file %> 
      </p> 
  <p><%= f.submit "Upload" %></p> 
<% end %>


[资产/new.html.erb]

<% title "Upload a file" %> 

<%= render 'form' %> 

<p> 
   <%= link_to "Back", root_url %> 
</p>


3[迁移]

class CreateAssets < ActiveRecord::Migration
  def change
    create_table :assets do |t|
           t.integer :user_id
           t.timestamps
    end
    add_index :assets, :user_id
  end
end


4[迁移​​]

class AddAttachmentUploadedFileToAssets < ActiveRecord::Migration
  def change
    change_table :assets do |t|
        add_column :assets, :uploaded_file_file_name, :string
        add_column :assets, :uploaded_file_content_type, :string
        add_column :assets, :uploaded_file_file_size, :integer
        add_column :assets, :uploaded_file_updated_at, :datetime
    end
  end


end

]4

【问题讨论】:

  • 完整的错误是什么?
  • 我已经提到了现在只允许的错误方法.....
  • 当我试图点击上传按钮时,它只是说
  • 能发个截图什么的吗?我真的需要查看错误的上下文
  • 好的,我在这里发帖

标签: ruby-on-rails paperclip


【解决方案1】:

问题在于 OP 引用了 /assets,它被 Rails 保留为公共路径。

因此,解决方案是至少将路线更改为/assets以外的其他内容; Model 可以保留,但控制器可能也必须更改:

#config/routes.rb
resources :assets, path: "asset", only: [:new, :create] #-> url.com/asset/new

【讨论】:

    【解决方案2】:

    如果您不是存储桶所有者,但对存储桶拥有 PutBucketPolicy 权限,Amazon S3 将返回 405 Method Not Allowed。 (来自S3 documentation

    假设您按照教程使用 S3,可能您没有在初始化程序中正确设置 S3 密钥。

    【讨论】:

    • 我没有使用 amazon s3...我正在尝试上传到我的本地机器 ....这就是我收到该错误的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2018-05-06
    • 2010-12-31
    • 2014-07-13
    • 2012-10-28
    相关资源
    最近更新 更多