【发布时间】:2015-11-14 08:47:26
【问题描述】:
我正在使用回形针 gem 将文件上传到数据库。当我选择要上传的文件并创建它时,下一页应该重定向到根路径。相反,我在浏览器中得到“不允许的方法”。我打开开发工具,控制台说: 加载资源失败:服务器响应状态为 405(不允许方法) 我的日志看起来像:
Started POST "/assets" for ::1 at 2015-08-20 10:41:11 -0400
在我返回另一个页面之前一直保持着迷状态。
这是我的控制器
class AssetsController < ApplicationController
# before_filter :authenticate_user!
def index
@assets = current_user.assets
end
def show
@asset = current_user.assets.find(params[:id])
end
def new
@asset = current_user.assets.build
end
def create
@asset = current_user.assets.build(asset_params)
if @asset.save
flash[:success] = "The file was uploaded!"
redirect_to root_path
else
render '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 asset_params
params.require(:asset).permit(:uploaded_file)
end
end
这是我的模型:
class Asset < ActiveRecord::Base
belongs_to :user
has_attached_file :uploaded_file
validates_attachment_presence :uploaded_file
validates_attachment_size :uploaded_file, less_than: 10.megabytes
end
我正在使用 simple_form gem 提交文件:
<%= simple_form_for @asset, :html => {:multipart => true} do |f| %>
<% if @asset.errors.any? %>
<ul>
<% @asset.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
</ul>
<% end %>
<% end %>
<p>
<%= f.input :uploaded_file %>
</p>
<p>
<%= f.button :submit, class: "btn btn-primary" %>
</p>
<% end %>
405 错误表明我正在执行资产模型不支持的请求。我的配置路由文件中有资源 :assets ,当我运行 rake 路由时,我所有的 RESTful 路由都在那里。
我将不胜感激。
【问题讨论】:
标签: ruby-on-rails forms paperclip http-status-code-405