I'm having an issue with some routes in my apps, the apps are the same
app the only difference is that I use one for testing and the other for
production stored in different directories lets say 'test' and 'prod'.
The applications work with phusion passenger and their work in a virtual
host.
The thing is that in my routes.rb I have this code for both apps:
map.connect '', :controller => 'controllerName'
It seems that all worked just fine until I tested a form and the
resulting url was something like this: www.name.com/controllerName when
the correct url must be something like this
www.name.com/test/controllerName.
The form that give me this problem is this one:
form action="../controllerName/" method="post"
form action="/controllerName/" method="post"
I guess this will happen with every form that redirect to the controller
set in the map.connect like the default controller.
I hesitate to reply because I do not fully understand what you are
doing so anything I say may be rubbish. However I believe that the
url generated by a commit action of a form (which I think is what you
are referring to) is nothing to do with anything in routes.rb, it is
purely determined by the html of the page and, if it is a relative
url, then the url of the current page. The routes.rb file is only
referenced after the post is received to determine where to send it.
Secondly you say that the form giving the problem is "this one" then
show us two form tags.
Thirdly, and this is the bit I don't understand and so hope to learn
more, why are you specifying the controller name in the action and why
the ../ or / on the front?
I hesitate to reply because I do not fully understand what you are
doing so anything I say may be rubbish. However I believe that the
url generated by a commit action of a form (which I think is what you
are referring to) is nothing to do with anything in routes.rb, it is
purely determined by the html of the page and, if it is a relative
url, then the url of the current page. The routes.rb file is only
referenced after the post is received to determine where to send it.
Secondly you say that the form giving the problem is "this one" then
show us two form tags.
Thirdly, and this is the bit I don't understand and so hope to learn
more, why are you specifying the controller name in the action and why
the ../ or / on the front?
Colin
Hi Colin,
for example this other form don't give this problem
<form action="../report/c" method="post">
when I submit this form the route is correct lets say
www.name.com/test/report/c
and with the other form the problem is that the /test/ is omitted so it
lead to a page that doesn't exist.
Thirdly, and this is the bit I don't understand and so hope to learn
more, why are you specifying the controller name in the action and why
the ../ or / on the front?
The ../ or / at the beginning are used for being certain that the url
it's created from the root of the application, I found that when using
html form tags in rails you need to add this to point to the right
direction but apparently it's only needed when the controller is a
different from the default controller.
When you use that syntax directing the commit to the default controller
you get the problem i'm having...
So the solution is to write the form in this way if you try to reach an
action inside the default controller.
Also the controller name is need or you will get something like this
www.name.com/action
and unless you are aiming to a action in the default controller you will
get an error there or in my case if you have multiple RoR applications
in the same server.
Thirdly, and this is the bit I don't understand and so hope to learn
more, why are you specifying the controller name in the action and why
the ../ or / on the front?
The ../ or / at the beginning are used for being certain that the url
it's created from the root of the application, I found that when using
html form tags in rails you need to add this to point to the right
direction but apparently it's only needed when the controller is a
different from the default controller.
When you use that syntax directing the commit to the default controller
you get the problem i'm having...
So the solution is to write the form in this way if you try to reach an
action inside the default controller.
Also the controller name is need or you will get something like this
www.name.com/action
and unless you are aiming to a action in the default controller you will
get an error there or in my case if you have multiple RoR applications
in the same server.
OK, I have just realised (stupidly) that you are not using the rails
form helpers (form_tag or form_for).
When you say that it goes to the wrong url do you mean that POST goes
to the wrong url or after doing the POST action it then goes to the
wrong url.
Are you able to show me the html generated by the form tag (view the
page source in the browser and copy and paste the relevant html code
here. Also look in the rails log (log/development.log) and copy and
paste the relevant section for clicking the submit button.