从Rails 3升级后不允许使用Rails 4方法 [英] Rails 4 Method Not Allowed after Upgrading from Rails 3
问题描述
我有一个现有的代码库,我正在尝试从Rails 3.2升级到Rails 4.0
I've got an existing codebase that I'm attempting to upgrade from Rails 3.2 to Rails 4.0
我有一个名为assets_controller的控制器,带有'create'方法,我的路线文件中有一个条目:
I have controller called assets_controller with a 'create' method and I have a an entry in my routes file:
resources :assets
在前端使用jQuery for ajax,如果我从浏览器向'/ assets'发送帖子请求,我得到405(方法不允许):
Using jQuery for ajax on the front end, if I send a post request to '/assets' from a browser, I get 405 (Method Not Allowed):
$.ajax({method: 'POST', data: asset, url: '/assets' });
这在Rails 3中运行得很好,我似乎无法弄清楚问题是什么。
This worked just fine in Rails 3, and I can't seem to figure out what the issue is.
更新:
以下是我的控制器的简化版本:
Heres a simplified version of my controller:
class AssetsController < ApplicationController
skip_before_filter :verify_authenticity_token
def create
# params[:assets] is passed if a mass addition of assets (i.e. book) occurs
assets = []
if params[:assets]
assets = params[:assets]
else
assets.push params
end
last_asset_id = 0
assets.each do |asset_data|
asset = Object.const_get(asset_data[:asset_type]).new(asset_data)
if !asset.save
json_false_errors(asset.errors.full_messages)
return
else
last_asset_id = asset.id
end
end
end
end
继承人'rake routes'的输出
Heres the output from 'rake routes'
assets GET /assets(.:format) assets#index
POST /assets(.:format) assets#create
new_asset GET /assets/new(.:format) assets#new
edit_asset GET /assets/:id/edit(.:format) assets#edit
asset GET /assets/:id(.:format) assets#show
PATCH /assets/:id(.:format) assets#update
PUT /assets/:id(.:format) assets#update
DELETE /assets/:id(.:format) assets#destroy
继承我的开发日志:
Started POST "/assets" for 127.0.0.1 at 2015-05-27 09:39:42 -0400
(是的,这就是所有日志都有)
(yeah thats all the log has)
POST DATA:
{
asset_type:文档,
标题:DNS,
heading_id:9999,
受版权保护:false,
url: https://confidental.url ,
pubtitle:DNS,
作者:}
POST DATA: { "asset_type":"Document", "title":"DNS", "heading_id":9999, "copyrighted":false, "url":"https://confidental.url", "pubtitle":"DNS", "author":""}
另一个编辑:
我将整个路线文件注释掉以用于诊断目的s,这些是进行一些手动测试的结果:
Another I commented out my entire routes file for diagnostic purposes, these are the results of doing some manual testing:
POST http://localhost:8000/assets 405 (Method Not Allowed)
POST http://localhost:8000/asset 404 (Not Found)
POST http://localhost:8000/ass 404 (Not Found)
是rails 4中某种保留端点的资产吗?
is assets some sort of reserved endpoint in rails 4?
推荐答案
这不仅仅是关于资产
这个词。当路径路径和资产目录位于同一子目录中时,Rails不喜欢。
This isn't just about the word assets
. Rails does not like when a route path and the asset directory are in the same subdirectory.
在发布帖子请求时,您将获得方法允许
。问题是路径和资产目录不能重叠。问题特别在于该路径中的 POST
请求。我假设在rails中的某个地方,他们必须已禁用资产目录的所有非GET请求。
When making a post request, you will get method not allowed
. The problem is there can be no overlap with paths and the asset directory. The problem is specifically with POST
requests in that path. I am assuming somewhere in rails, they must have disabled all non-GET requests for the assets directory.
在此非常下面简单的应用程序,你将得到方法不允许
错误。因为路径 / welcome
用于路由和资产前缀。
In this very simple app below, you will get a method not allowed
error. Because the path /welcomes
is being used for a route and for an asset prefix.
文件: config / environment / development.rb
File: config/environment/development.rb
config.assets.prefix = '/welcomes'
文件: config / routes.rb
File: config/routes.rb
resources :welcomes, path: 'welcomes', only: ['index', 'create']
文件: app / controllers / welcome_controller.rb
File: app/controllers/welcomes_controller.rb
class WelcomesController < ApplicationController
def index
@welcome = 'hello';
end
def create
@welcome = 'world';
end
end
文件: app / views / welcome / index.html.rb
File: app/views/welcomes/index.html.rb
<%= form_for(@welcome) do |f| %>
<%= f.submit 'Submit' %>
<% end %>
文件: app / views / welcome / create.html。 rb
File: app/views/welcomes/create.html.rb
<h1>Welcomes#create</h1>
<p>Find me in app/views/welcomes/create.html.erb</p>
这篇关于从Rails 3升级后不允许使用Rails 4方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!