Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form_tag url_for_options nil action to the controller itself

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?

like image 760
Luca Milan Avatar asked Feb 18 '14 11:02

Luca Milan


3 Answers

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.

like image 54
Benissimo Avatar answered Nov 05 '22 10:11

Benissimo


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

like image 38
Luca Milan Avatar answered Nov 05 '22 12:11

Luca Milan


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.

like image 31
Mandeep Singh Avatar answered Nov 05 '22 12:11

Mandeep Singh