My case is that i have more controllers with sharing one view and i'm trying to change dinamycally the action on a form passing a variable defined in the controller
Trying some solution i've observed that if i have a nil variable in url_for_options form_tab variable, the form in the view has the right controller path from where the view was called
<%= form_tag nil, :method => :get, :class => 'search' do %>
is it a bug or a feature?
Note that if you pass an empty string, instead of nil, you get a slightly different behavior:
form_tag nil
creates
<form action="(URL based on current controller and action)">....
While
form_tag ""
creates
<form action>....
If the URL of the page you're on is, say, "/foo/bar" (for FooController#bar), then the two are functionally equivalent. If the URL of the current page is "/foo/bar?a=1&b=2", the former will continue to point at just "/foo/bar", while the latter will result in a form that submits to the full URL (querystring included). This is because the HTML standard (no action or empty action = use current URL, including querystring) is used by the latter. Instead the former triggers a Rails standard (nil for url_for => use current controller and method).
Just thought it's worth mentioning as there are cases where the HTML standard for an action-less form is more useful (ie. where you want querystring parameters to be sticky), and the subtle difference in behavior between nil and "" is easy to miss.
The solution is using an empty action attribute on a form to submit that form to the current page. and is described on RFC 2396
4.2. Same-document References
A URI reference that does not contain a URI is a reference to the
current document. In other words, an empty URI reference within a
document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference
to the identified fragment of that document. Traversal of such a
reference should not result in an additional retrieval action.
However, if the URI reference occurs in a context that is always
intended to result in a new request, as in the case of HTML's FORM
element, then an empty URI reference represents the base URI of the
current document and should be replaced by that URI when transformed
into a request.
A strange behaviour when
url_for_options == nil
<form accept-charset="UTF-8" action="/welcome" class="search" method="get">
url_for_options == ""
<form accept-charset="UTF-8" action class="search" method="get">
It is not a bug. If you don't provide any action to 'form_tag' or 'form_for', By default it will be posted to same controller where view is called from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With