Form not invoking "create" action in production

I’ve officially lost my mind. I’ve got a form that looks like this:

<%= error_messages_for :avatar %>
<% form_for(:avatar, :url => avatars_path, :html => { :multipart =>
true }) do |f| %>

<%= f.file_field :uploaded_data %>

<%= submit_tag "Upload" %> <% end %>

It uses attachment_fu to allow users to upload avatars. Everything
works fine when I’m in development, but in production the “create”
action is not invoked from some reason.

I’m using standard RESTful routes, like this:

map.resources :avatars

And I’ve got a “create” action in my avatars controller:

def create
@avatar = current_user.avatars.build params[:avatar]
respond_to do |format|
if @avatar.save
flash[:notice] = ‘Avatar was successfully created.’
format.html { redirect_to avatars_path }
format.xml { head :created, :location => avatars_path }
else
format.html { render :action => “new” }
format.xml { render :xml => @avatar.errors.to_xml }
end
end
end

In development, everything works as expected. My production.log file
shows me this following when I try to upload a new avatar:

Processing AvatarsController#new (for 203.98.22.198 at 2007-04-02
21:11:35) [GET]
Parameters: {“action”=>“new”, “controller”=>“avatars”}
Rendering within layouts/application
Rendering avatars/new
Completed in 0.15629 (6 reqs/sec) | Rendering: 0.01185 (7%) | DB:
0.14027 (89%) | 200 OK

Processing AvatarsController#index (for 203.98.22.198 at 2007-04-02
21:11:45) [GET]
Parameters: {“action”=>“index”, “controller”=>“avatars”}
Rendering content_typetext/htmlactionindexlayoutfalse within layouts/
application
Rendering avatars/index
Completed in 0.13149 (7 reqs/sec) | Rendering: 0.06164 (46%) | DB:
0.06527 (49%) | 200 OK

In development, things looks right, though:

Processing AvatarsController#new (for 127.0.0.1 at 2007-04-03
16:14:50) [GET]
Parameters: {“action”=>“new”, “controller”=>“avatars”}
Rendering within layouts/application
Rendering avatars/new
Completed in 0.93098 (1 reqs/sec) | Rendering: 0.23151 (24%) | DB:
0.15408 (16%) | 200 OK

Processing AvatarsController#create (for 127.0.0.1 at 2007-04-03
16:14:57) [POST]
Parameters: {“commit”=>“Upload”, “action”=>“create”,
“controller”=>“avatars”, “avatar”=>{“uploaded_data”=>#<File:/tmp/
CGI3922-0>}}
Redirected to http://localhost:3000/avatars
Completed in 0.54049 (1 reqs/sec) | DB: 0.06105 (11%) | 302 Found

Processing AvatarsController#index (for 127.0.0.1 at 2007-04-03
16:14:59) [GET]
Parameters: {“action”=>“index”, “controller”=>“avatars”}
Rendering content_typetext/htmllayoutfalseactionindex within layouts/
application
Rendering avatars/index
Completed in 0.96327 (1 reqs/sec) | Rendering: 0.49423 (51%) | DB:
0.05750 (5%) | 200 OK

I just don’t get why the “create” action isn’t being invoked in
production with this code. Is there something wrong with my form that
I’m missing? I can’t seem to figure this one out.

Any help would be greatly appreciated.

I’ve gotten a response from Media Temple on this, although I’m still
not sure how I’ll resolve the issue. I’m sure I’m not the first or
last person who’ll try to do what I’m doing, so I’m hopeful that I’ll
find a solution. Here’s what they said:

The problem you are running into is not caused by code or
configuration, but coincidence. Your controller is exposed as “/
files”
and you have a directory in your public folder called files.

When your form is created, the action is “/files”. When this post
request hits the Grid, Apache sees that request (http://app.com/
files)
and that there is a directory there, and so appends a slash (http://
app.com/files/) and redirects the browser to the new URL. This is
standard Apache behavior since most requests for a directory name are
usually for the directory’s contents rather than the directory
itself.
Since Apache does the redirect, the post turns into a get before your
Rails application even has a chance to see it. To test this out,
rename your files directory to any other name and you will see that
the post request starts working again.

And, seeing that you are also having issues with the /headers and /
avatars and that there are matching directories in your public
directory, the same issue is probably happening there.


If anyone has an idea of how to work around this behavior (changing
the Apache config?) please let me know. Otherwise, I’ll update this
post when I figure out a solution.

  • Trevor

If anyone has an idea of how to work around this behavior (changing
the Apache config?) please let me know. Otherwise, I’ll update this
post when I figure out a solution.

Their advice is right on, and makes sense once I read it. I had the
same problem, and the only thing I can think of is that Mike C.
wasn’t using apache when he wrote that tutorial.

Anyway, to work around this you need to use the :path_prefix option in
your has_attachment statement inside your model:

Begin model

class Mugshot < ActiveRecord::Base

has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
:resize_to => ‘320x200>’,
:thumbnails => { :thumb => ‘100x100>’ },
:path_prefix => “public/something_besides_avatars”

validates_as_attachment

end

end model

This will prevent apache from hijacking the request because the
directory you created to hold the images won’t match the rails action
you’re trying to invoke. Once I changed that setting things started
working for me.

Does that help?

Actually, I’ve added the following to my .htaccess:

DirectorySlash Off

… which solves the problem. You can read more about that here:

http://httpd.apache.org/docs/2.0/mod/mod_dir.html

I was wondering if there’s any reason why this shouldn’t be a part of
the default Rails .htaccess file…?

  • Trevor

On May 4, 4:50 pm, Sam Storie [email protected] wrote:

:path_prefix => “public/something_besides_avatars”