Rails 4 Method Not Allowed after Upgrading from Rails 3

不问归期 提交于 2019-12-05 04:16:47

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.

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.


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.

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>

It turns out the problem was with the name 'assets' I can't find any documentation to confirm this, but renaming the asset model and controller to something else fixed the problem.

The issue is that your asset controller routes are conflicting with the rails default /assets path.

The simplest solution is to modify your config/routes.rb file line to read as follows (or any other path of your choosing that is not assets):

resources :assets, path: 'site_assets'

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