Post returns 405 method not allowed
Asked Answered
L

3

22

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
Lynda answered 14/7, 2015 at 16:48 Comment(2)
Tyler, are you using any csrf protection?Glaive
I am using rails 4, I did not add csrf protection that I know of. @AdamLieskovskýLynda
V
54

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

Vashti answered 22/1, 2016 at 7:5 Comment(0)
W
1

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
Weiman answered 14/7, 2015 at 19:4 Comment(0)
S
1

In the routes.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.

Singsong answered 10/7, 2018 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.