Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2726 (closed defect: wontfix)

Opened 4 years ago

Last modified 2 years ago

Form helpers should not require an instance variable

Reported by: nolan@eakins.net Assigned to: David
Priority: normal Milestone: 1.2.4
Component: ActionPack Version: 0.14.1
Severity: normal Keywords: form helpers instancetag partials collections
Cc:

Description

I was working with rendering collections with partials last night. When you render a collection w/ a partial a variable is created that's the same as the partial, ie: "_user.rhtml" creates the variable "user".

The form helpers assume the object you want them to reference is an instance variable, ie: they require an '@'. This makes them completely useless in a partial unless I explicitly include something like "<% @user = user %>" at the top of each partial.

I'm sure the assumption that the form helper would reference an instance variable is to make it so I don't have to type a bunch of '@'s. This follows the DRY principle to an extent, but forces a much much greater violation of DRY. That violation is the inclusion of an instance variable assignment in /every/ single partial that uses a form helper.

Obviously backwards compatibility is important, so the form helpers would have to see if an instance variable exists, and if it does not then it needs to try a local variable. I would also be fine w/ using "text_field '@user', 'name'" for every call to a form helper. It's more explicit and doesn't assume what kind of variable I want.

Change History

11/20/05 10:12:34 changed by dreamer3@gmail.com

Maybe when rendering partials assign to both user and @user? Though i'm betting there is some reason instance vars weren't used in the first place.

11/23/05 14:38:51 changed by jeroen@terena.nl

Yes intuitively I tried doing:

<%= check_box(person, "active", {}, true, false)%>

I don't understand why the first paramter has to be a string. Couldn't the method be rewritten so the first parameter accepts any object that responds to a method with the name of 2nd parameter?

11/23/05 21:07:50 changed by me@julik.nl

  • priority changed from high to low.

No, because the string is used to construct the params hash. The Rails convention implies that you use the instance variable name of the form helper as the key of the params that will be returned from the form. That means that the first argument has to be a string, which does 2 things - hooks up the object from the instance variable and assigns the passed string to a key in the form being filled in.

What you indeed can achieve is pass a string and make the helpers lookup the needed object in the parent calling block as an instance variable, then as a method and then as a variable.

11/24/05 03:31:15 changed by nolan@eakins.net

  • priority changed from low to normal.

My problem with this is that I have to put something like "<% @user = user %>" in every partial that calls a form helper. From the previous comment it appears that the form helpers need an instance variable.

How about partials? If I render "_user.rhtml", can it create "@user" instead of "user"? That'll save me tons of typing by keeping things dry, or am I missing something?

05/23/07 03:44:02 changed by nik.wakelin

  • status changed from new to closed.
  • resolution set to wontfix.

Closing stale ticket, workaround in form_for() and I'm fairly sure this won't change. Reopen if you disagree ;)