Rails无法使用回形针上传文件

时间:2016-01-29 07:28:30

标签: ruby-on-rails paperclip

enter image description here enter image description here我正在关注此tutorial


我正在尝试使用paperclip gem上传文件..当我尝试上传文件时出现错误方法不允许这就是为什么我的资产表为空白没有数据插入

[asset.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


[user.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

upload using paperclip[![][2]] 4

2 个答案:

答案 0 :(得分:1)

  

如果您不是存储桶拥有者但在存储桶上具有PutBucketPolicy权限,则Amazon S3将返回405方法不允许。   (来自S3 documentation

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

答案 1 :(得分:1)

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

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

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