【发布时间】: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
【问题讨论】:
-
完整的错误是什么?
-
我已经提到了现在只允许的错误方法.....
-
当我试图点击上传按钮时,它只是说
-
能发个截图什么的吗?我真的需要查看错误的上下文
-
好的,我在这里发帖