尝试在Rails中发布文件时出现405错误 [英] 405 Error when trying to POST a file in rails

查看:278
本文介绍了尝试在Rails中发布文件时出现405错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回形针gem将文件上传到数据库.当我选择要上传的文件并创建文件时,下一页应将redirect_to到根路径.取而代之的是,我在浏览器中看到不允许使用方法".我打开开发工具,控制台显示: 无法加载资源:服务器的响应状态为405(不允许使用方法) 我的日志如下:

I am using paperclip gem to upload a file to the database. When I select the file I want to upload and go to create it, the next page should redirect_to root path. Instead, I get "Method Not Allowed" in my browser. I open Dev Tools and the console says: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) My logs look like:

Started POST "/assets" for ::1 at 2015-08-20 10:41:11 -0400

并且一直迷住了,直到我返回另一页.

and remains hooked until I go back to another page.

这是我的控制人

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提交文件:

And I am using the simple_form gem to submit the file:

<%= 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路由都在那里.

The 405 error indicates that I am doing a request not supported by the Asset model. I have resources :assets in my config routes file and when I run rake routes, all of my RESTful routes are there.

在此方面的任何帮助,我将不胜感激.

I would appreciate any help on this one.

推荐答案

首先引起我注意的是,您在/assets有一条路由,通常保留给Rails issue 10132 和<在Rails核心上访问a href ="https://github.com/rails/rails/issues/19996" rel ="noreferrer">问题19996 .

The first thing that comes to my attention is that you have a route at /assets, which is normally reserved to the Rails asset pipeline. Rails is known to be not very informative about reporting problems with this (see issue 10132 and issue 19996 on rails core).

尝试将资产管道移动到另一个安装点,看看是否可以解决您的问题.这可以通过将Rails.application.config.assets.prefix设置为/assets以外的其他值来实现.例如,在config/initializers/assets.rb中添加以下行:

Try moving your asset pipeline to another mount point, and see if that fixes your problem. This can be accomplished by setting Rails.application.config.assets.prefix to something other than /assets. For example in config/initializers/assets.rb add the line:

Rails.application.config.assets.prefix = '/pipeline_assets'

这篇关于尝试在Rails中发布文件时出现405错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆