Rails 4 method not allowed after upgrade from Rails 3

I have an existing code base that I am trying to upgrade from Rails 3.2 to Rails 4.0

I have a controller called assets_controller with the create method, and I have an entry in my routes file:

resources :assets 

Using jQuery for ajax on the frontend, if I send a request to publish in the / assets browser, I get 405 (method not allowed):

 $.ajax({method: 'POST', data: asset, url: '/assets' }); 

This worked well in Rails 3, and I can't figure out what the problem is.

Updates:

Here is 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 (ie 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 

Here is the way out of 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 

Here is my development log:

 Started POST "/assets" for 127.0.0.1 at 2015-05-27 09:39:42 -0400 

(yes, that's the whole magazine)

POST DATA: {"asset_type": "Document", "title": "DNS", "heading_id": 9999, "copyrightted": false, "url": " https: //confidental.url ", "pubtitle": "DNS", "author": ""}

Another edit: I commented out my entire route file for diagnostic purposes, here are the results of 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) 

Are assets a kind of reserved endpoint on rails 4?

+7
jquery ajax ruby-on-rails-4 routes
source share
3 answers

This is not just the word assets . Rails doesn't like it when the route path and resource directory are in the same subdirectory.

When you create a mail request, you will receive method not allowed . The problem is that there can be no overlap with the paths and resource directory. The problem is precisely with POST requests in this way. I assume that somewhere on the rails, they should have disabled all non-GET requests for the resource directory.


In this very simple application below you will get a method not allowed error. Because the path /welcomes used for the route and for the asset prefix.

File: config/environment/development.rb

 config.assets.prefix = '/welcomes' 

File: config/routes.rb

 resources :welcomes, path: 'welcomes', only: ['index', 'create'] 

File: app/controllers/welcomes_controller.rb

 class WelcomesController < ApplicationController def index @welcome = 'hello'; end def create @welcome = 'world'; end end 

File: app/views/welcomes/index.html.rb

 <%= form_for(@welcome) do |f| %> <%= f.submit 'Submit' %> <% end %> 

File: app/views/welcomes/create.html.rb

 <h1>Welcomes#create</h1> <p>Find me in app/views/welcomes/create.html.erb</p> 
+5
source share

It turned out that the problem is connected with the name โ€œassetsโ€, I canโ€™t find the documentation confirming this, but renaming the model and the asset controller to something else fixed the problem.

+8
source share

The problem is that your asset controller routes contradict the default paths /assets by default.

The simplest solution is to change your config/routes.rb line string as follows (or any other path of your choice that is not assets ):

resources :assets, path: 'site_assets'

+1
source share

All Articles