Post returns 405 method not allowed

前端 未结 3 1837
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 02:01

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

相关标签:
3条回答
  • 2021-02-19 02:27

    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

    0 讨论(0)
  • 2021-02-19 02:35

    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.

    0 讨论(0)
  • 2021-02-19 02:37

    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
    
    0 讨论(0)
提交回复
热议问题