# Form not invoking "create" action in production

**URL:** <https://www.ruby-forum.com/t/form-not-invoking-create-action-in-production/90942>\
**Category:** Rails\
**Created:** [April 3, 2007, 6:18am UTC](https://www.ruby-forum.com/t/form-not-invoking-create-action-in-production/90942 "2007-04-03T06:18:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevorturk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@trevorturk](https://www.ruby-forum.com/u/trevorturk)\
**Post date:** [April 3, 2007, 6:18am UTC](https://www.ruby-forum.com/t/form-not-invoking-create-action-in-production/90942/1 "2007-04-03T06:18:18Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![trevorturk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@trevorturk](https://www.ruby-forum.com/u/trevorturk)\
**Post date:** [April 5, 2007, 2:15am UTC](https://www.ruby-forum.com/t/form-not-invoking-create-action-in-production/90942/2 "2007-04-05T02:15:29Z")

</div>

## 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/](http://app.com/)  
files)  
and that there is a directory there, and so appends a slash (http://  
[app.com/files/](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

---

<div class="post-metadata">

**Author:** ![trevorturk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@trevorturk](https://www.ruby-forum.com/u/trevorturk)\
**Post date:** [May 4, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/form-not-invoking-create-action-in-production/90942/3 "2007-05-04T23:50:54Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![trevorturk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@trevorturk](https://www.ruby-forum.com/u/trevorturk)\
**Post date:** [May 29, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/form-not-invoking-create-action-in-production/90942/4 "2007-05-29T20:42:23Z")

</div>

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](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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> :path\_prefix =\> “public/something\_besides\_avatars”
