Suddenly getting 405 errors when using patch or put methods

43 views
Skip to first unread message

Walter Lee Davis

unread,
May 2, 2015, 3:51:05 PM5/2/15
to rubyonra...@googlegroups.com
I am working in a simple test application with Paperclip, and while I am able to upload in a #create, I am getting 405 errors from Webrick and Thin (in development) when trying to use the #update method.

I don't even get to the point in my console where I see the params -- it's happening before that. It's not a Rails error message, it's coming directly from the server.

I started in 4.2.1 and Ruby 2.1.3, and I have tried the entire matrix of 4.2.0 and 2.1.2 also without any change. I also tried both Chrome and Safari without any difference. I'm using Mac OS X 10.9.5 and rvm, if that makes any difference.

Thanks,

Walter

Colin Law

unread,
May 3, 2015, 3:48:39 AM5/3/15
to rubyonra...@googlegroups.com
On 2 May 2015 at 20:50, Walter Lee Davis <wa...@wdstudio.com> wrote:
> I am working in a simple test application with Paperclip, and while I am able to upload in a #create, I am getting 405 errors from Webrick and Thin (in development) when trying to use the #update method.
>
> I don't even get to the point in my console where I see the params -- it's happening before that. It's not a Rails error message, it's coming directly from the server.

Assuming you run webrick in a terminal, what do you see in that
terminal when you submit the request?

Colin

Walter Lee Davis

unread,
May 3, 2015, 9:46:28 AM5/3/15
to rubyonra...@googlegroups.com
Started PATCH "/assets/1" for ::1 at 2015-05-02 15:32:49 -0400

The browser shows plain text:

Method Not Allowed

and nothing else. It never hits ActiveRecord or even ActionController, as best I can see.

I can go back and reload and see the form page (and that is nothing remarkable -- I made it with the scaffold). I can view the index and use #create to make another such, but if I try to edit it, I get the error.

Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuTXfu61u%2B1RYZ9buJUEj8ShpGS4JWn9DRgAY3FcjJ7jg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
May 3, 2015, 10:13:48 AM5/3/15
to rubyonra...@googlegroups.com
On 3 May 2015 at 14:45, Walter Lee Davis <wa...@wdstudio.com> wrote:
> Started PATCH "/assets/1" for ::1 at 2015-05-02 15:32:49 -0400

Have you got a model called Asset? I suspect that might cause
confusion with assets (js, css etc).
Is it only with assets that you get the problem?

Colin

Walter Lee Davis

unread,
May 3, 2015, 10:40:23 AM5/3/15
to rubyonra...@googlegroups.com
I'll try renaming it, but I have used this pattern (and names) on many applications (this test app is an extraction from a working commercial application of mine, although that uses 4.1.8) and I am just trying to stub out multiple file uploads in this exercise.

I just re-built my environment -- re-installed rvm, ruby 2.1.3, bundler, rake, rails 4.2.1, and dropped a couple of gems that looked suspicious -- and started completely over with scaffold again. I am getting the identical problem.

If it's just the name of the model, I will be frankly surprised, because each revision of Rails seems to remove reserved words, not add new ones.

While I do that, here's the actual code -- if you can see anything suspicious, I'd be very grateful.

class Asset < ActiveRecord::Base
belongs_to :project
has_attached_file :blob, :styles => { :large => "800x800", :medium => "400x400>", :small => "200x200>"}
do_not_validate_attachment_file_type :blob
end

class Project < ActiveRecord::Base
has_many :assets
accepts_nested_attributes_for :assets, reject_if: :all_blank, allow_destroy: true
end

Rails.application.routes.draw do
resources :projects
resources :assets
end

The pattern I am trying to solve for is upload multiple assets in the project form (this works perfectly) and edit individual uploaded assets in their own assets/edit form. This latter part does not work.

This is the form that is failing:

<%= form_for(@asset) do |f| %>
<% if @asset.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@asset.errors.count, "error") %> prohibited this asset from being saved:</h2>

<ul>
<% @asset.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :project_id %>
<%= f.text_field :project_id %>
</div>
<div class="field">
<%= f.label :blob %>
<%= f.file_field :blob %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

While this one works just fine:

<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<%= f.fields_for :assets do |a| %>
<div class="field">
<%= a.label :blob %>
<%= a.file_field :blob, multiple: true, name: "project[assets_attributes][][blob]" %>
</div>
<%- end -%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsfyN983fSdOybQxazfRQ1LtgVgAqL0SrpOtHhJFfXWVA%40mail.gmail.com.

Walter Lee Davis

unread,
May 3, 2015, 10:53:07 AM5/3/15
to rubyonra...@googlegroups.com

On May 3, 2015, at 10:40 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:

> If it's just the name of the model, I will be frankly surprised, because each revision of Rails seems to remove reserved words, not add new ones.

Thanks, Colin, for being able to state the obvious. Changing the model name made all the difference. Good to know, because I was starting to have some trust issues here...

Walter

Colin Law

unread,
May 3, 2015, 11:29:41 AM5/3/15
to rubyonra...@googlegroups.com
I think the reason is that assets are served directly by the web
server, hence the request did not get to rails at all.

Colin

Walter Lee Davis

unread,
May 3, 2015, 11:40:14 AM5/3/15
to rubyonra...@googlegroups.com
That doesn't jibe with what I am seeing in the rest of the application. I can GET them, and they go all the way through Rails, I can index them, and I can POST to the collection and create a new asset. It's just PATCH and PUT on the individual asset object that are affected.

I agree that changing the name is the expedient fix (although it's going to be hard to find another "neutral" name for "sub-project element that might be an image or a Flash banner ad or H264 movie"). I guess I'll call them Element, unless you have another suggestion.

Thanks again for your help,

Walter

>
> Colin
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs4_u1bELXnSccPi%3DwoRvEnX36ocJAUQZ9xWkea%3DV2-pA%40mail.gmail.com.

Hassan Schroeder

unread,
May 3, 2015, 11:57:00 AM5/3/15
to rubyonrails-talk
On Sun, May 3, 2015 at 8:39 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:

> That doesn't jibe with what I am seeing in the rest of the application. I can GET them, and they go all the way through Rails, I can index them, and I can POST to the collection and create a new asset. It's just PATCH and PUT on the individual asset object that are affected.

Have you tried this running Thin in `trace` or `debug` mode? That
might be enlightening.

A simple test case to repro would be helpful, too.

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Walter Lee Davis

unread,
May 4, 2015, 7:13:41 AM5/4/15
to rubyonra...@googlegroups.com
I filed a bug report on rails/rails, and several people chipped in to help me locate the solution. Sprockets 3 is simultaneously greedy about the /assets route and silent about that fact. Adding this config statement:

Rails.application.config.assets.prefix = '/the-other-assets'

lets you get on with life when your model is named Asset.

Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yD6GCtyjRkQKA2%3DEbFGmXNBmNFxCmPCMjcbTMtw2VAXcQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages