Post returns 405 method not allowed

偶尔善良 提交于 2020-03-18 10:59:02

问题


I am having an issue with my Rails localhost Server, all Post calls started returning 405 method not allowed. However there are no problems on our staging and production servers. It is happening on all branches of code even ones that have not been updated. When debugging I see that it reaches the routes file but not the controller.

I have tried removing my gems and reinstalling, switching from WEBrick to Pama, creating a new clone of my git project.

Server

Started POST "/assets" for ::1 at 2015-07-14 12:14:27 -0400

Network Tab in Chrome

General

Remote Address:[::1]:3000
Request URL:http://localhost:3000/assets
Request Method:POST
Status Code:405 Method Not Allowed

Response

HTTP/1.1 405 Method Not Allowed
Cache-Control:no-cache
Content-Length:18
Content-Type:text/plain
X-Request-Id:9b0b2dd2-065b-4610-91c9-36494ea95353
X-Runtime:0.145368

Request

POST /assets HTTP/1.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8
    Cache-Control:no-cache
    Connection:keep-alive
    Content-Length:8376627
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryx8y8PBySdt7dxs4A
    Cookie:activeAccordionGroup=collapseTwo; _fusion360_hub_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWFlYmVlOGZjZmI3YzVlYjBjNjAyYzcyMzNhNzIyMzIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTllM2xhK1k4WG1hd2xYNnZCOEtHOEhPaHNTbWQvZGR2cGJ3bU9WUXIwRzg9BjsARg%3D%3D--4e108cb5f6eca3d986c0b3accec07bd2c27560b2; _mkto_trk=id:760-CWR-293&token:_mch-localhost-1435859445290-79614; _pk_id.845225.1fff=9847e7981c291a08.1435859445.1.1435859445.1435859445.; _allegorithmic-substance-marketplace_session=M3dMUCs4ZEtWSTFJTFVHV2VYN2pESFdHcGlHL2grVVVKSGxIWEZ3MlhXQkpRdHE2L0ZkMFpURmZDWGl6aTMxYSttMXFSQXN2M08zVVVXZTRHMDNKOHJOUzA1TmZoYnMwWURjb3c0Rkx6MTJYOW1Uem9aNGRObEMvc1NpSWo5VnQ4dUIzRnRtTFpnMlpOQVVZUU1SdWxiN1ZjN1lIMVd3Sk5jaXkyZkZLZ3duWTc4K2dnK0FSK29JVWdva2t0eUN1Q3hJbjFERHJVaGtndjVoWGxDRUlndz09LS1rZDdWcmtEWHlJWHRpZjc1MFNUSDF3PT0%3D--d34962721f449064dfdfd4629c0239ea1340aee4; __profilin=p%3Dt
    Host:localhost:3000
    Origin:http://localhost:3000
    Pragma:no-cache
    Referer:http://localhost:3000/assets/new
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36

回答1:


You have a route "assets", which is a reserved route in Rails and it is reserved for Rails Asset Pipeline.

If you must use the "assets" route then you need to give Asset Pipeline another mount point by adding the following line in your development.rb or production.rb configuration block:

config.assets.prefix = '/assetz'

And restart your server.

By this all your assets will be mounted on "/assetz" and you will be able to use POST requests on "/assets".

Happy coding!

Reference:

1) StackOverflow




回答2:


Could you post your routes file and also the exact rails version of your dev-environment and the production servers?

I assume this could happen when you post to a route that is only registered as a get request (depending on your rails version) or maybe routes that are defined twice, e.g.:

resources :photos, :only => [:index]
get :photos



回答3:


In the ruotes.rb file, include your routes in a namespace, so it should look like this:

Rails.application.routes.draw do
  namespace 'api' do
    Your routes here
  end
end

Then, add controllers corresponding to these routs in a folder with the same name of that namespace, so the app directory should look like this:

app
|  controllers
|  |  api
|  |  |  Your controller files here

Finally, the controllers should be inside a module with the same name of the namespace but with the first letter capital, so each controller should look like this:

  module Api
    Your controller code here
  end

Note: You can give each related set of routes/controllers different namespaces/modules. Also, you can use nested namespaces/modules.



来源:https://stackoverflow.com/questions/31412688/post-returns-405-method-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!