简体   繁体   English

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

[英]Rails unable to upload file using paperclip

在此处输入图片说明 在此处输入图片说明 I am following this tutorial 我正在关注本教程


I am trying to upload a file using paperclip gem ..When I try to upload a file i get an error Method Not Allowed Thats why my assets table is blank no data is inserting 我正在尝试使用回形针gem上传文件。当我尝试上传文件时,出现错误消息不允许使用方法,这就是为什么我的资产表为空而没有数据插入的原因

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

[assets/_form.html.erb] [assets / _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 %>


[assets/new.html.erb] [assets / new.html.erb]

<% title "Upload a file" %> 

<%= render 'form' %> 

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


3 [migration] 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 [migration] 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

使用回形针上载[![] [2] ] 4 ] 4

If you are not the bucket owner but have PutBucketPolicy permissions on the bucket, Amazon S3 returns a 405 Method Not Allowed. 如果您不是存储桶所有者,但对存储桶具有PutBucketPolicy权限,则Amazon S3返回405不允许的方法。 (From this S3 documentation ) (来自此S3文档

Assuming you're using S3 as per the tutorial, perhaps you have not set up your S3 keys properly in your initializer. 假设您按照教程使用S3,也许您没有在初始化程序中正确设置S3密钥。

The problem was that the OP was referencing /assets which is reserved by Rails as a public path. 问题是OP引用了/assets ,而Rails保留了/assets作为公共路径。

The solution would therefore be to change at least the routes to be something other than /assets ; 因此,解决方案是至少将路由更改 /assets以外的其他值; the Model can remain but the controller would probably have to change too: Model可以保留,但控制器可能也必须更改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM