FormBuilder object in a helper?

I want to DRY up my form-for.

#new.html.erb
<% form_for @organization, :url => organization_path do |f| %>
<%= f.label :church_name, “Church Name:” %>
<%= f.text_field :church_name,
:class => ‘input-field’,
:size => 30 %>
<%= error_message_on :organization, :church_name %>
<% end %>

with

#new.html.erb
<% form_for @organization, :url => organization_path do |f| %>
<%= text_field_with_errors( :church_name, “Church Name”) %>
<% end %>

organization_helper

def text_field_with_errors( f, display_name, size = 30 )
f.label( method, (display_name + ‘:’) ) <<
f.text_field( method,
:class => ‘input-field’,
:size => size ) <<
error_message_on( f, method )
end

But rails throws and error about can put a FormBuilder object into an
instance variable.

Not quite sure I understand. So, how would I DRY up this form?

On 20 Apr 2008, at 02:14, Karl S. wrote:

<% end %>

with

#new.html.erb
<% form_for @organization, :url => organization_path do |f| %>
<%= text_field_with_errors( :church_name, “Church Name”) %>
<% end %>

You’re not passing f to your helper function.

Fred

You’re not passing f to your helper function.

That was a typo on my part when I pasted/cleaned the code for posting.

#new.html.erb
<% form_for @organization, :url => organization_path do |f| %>
<%= text_field_with_errors( f, :church_name, “Church Name”) %>
<% end %>

The exact error is:
`@#ActionView::Base:0x24dda6c’ is not allowed as an instance
variable name

I thought you could pass any object to a method. Right?

On 20 Apr 2008, at 19:03, Karl S. wrote:

The exact error is:
`@#ActionView::Base:0x24dda6c’ is not allowed as an instance
variable name

I thought you could pass any object to a method. Right?
Well yes, but that method might not like it (eg trying to evaluate the
square root of “cucumber”). I think we need to see exactly what code
you’re running. You could have edited out the problem and it’s
difficult to work out what’s wrong if what we’re looking at isn’t what
is actually being executed.

Fred

Oops, slight change, I was trying to see if I could self. Doesn’t
really matter, either self or just f, neither works.

module OrganizationEventsHelper
def text_field_with_errors( f, method, display_name, size = 30,
options = {} )
f.label( method, (display_name + ‘:’) ) <<
f.text_field( method,
:class => ‘input-field’,
:size => size ) <<
error_message_on( f, method )
end
end

Well yes, but that method might not like it (eg trying to evaluate the
square root of “cucumber”). I think we need to see exactly what code
you’re running. You could have edited out the problem and it’s
difficult to work out what’s wrong if what we’re looking at isn’t what
is actually being executed.

Ok, there is the full code:

module OrganizationEventsHelper
def text_field_with_errors( f, method, display_name, size = 30,
options = {} )
self.label( method, (display_name + ‘:’) ) <<
self.text_field( method,
:class => ‘input-field’,
:size => size ) <<
error_message_on( self, method )
end
end

<% form_for @organization, :url => organization_events_path do |f| %>

Church Info
  1. <%= text_field_with_errors( f, :church_name, "Church Name") %>
<%= f.submit "Create" %> <% end %>

Note: I omitted the repetitious code.

Error message:
ActionView::TemplateError (`@#ActionView::Base:0x256474c’ is not
allowed as an instance variable name) on line #33 of
organization_events/new.html.erb:
30: :size => 30 %>
31: <%= error_message_on :organization, :church_name %>
32:
33:

  • <%= text_field_with_errors( f, :church_name, “Church
    Name”) %>

  • 34:
    35:
  • <%= f.label :address1, “Address:” %>
    36: <%= f.text_field :address1,
    config/initializers/field_error_patch.rb:29:in
    

    instance_variable_get' config/initializers/field_error_patch.rb:29:inerror_message_on’
    app/helpers/organization_events_helper.rb:9:in
    text_field_with_errors' app/views/organization_events/new.html.erb:33:in_run_erb_47app47views47organization_events47new46html46erb’
    /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/
    helpers/form_helper.rb:248:in fields_for' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_helper.rb:184:inform_for’
    app/views/organization_events/new.html.erb:22:in
    _run_erb_47app47views47organization_events47new46html46erb' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/ base.rb:637:insend’
    /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/
    base.rb:637:in `compile_and_render_template’

    Let me know if think the whole stack trace would help.
    Thanks for the help, Fred.

  • So the problem is with error_message_on. the version you called is
    expecting the first parameter to be the name of an instance variable.
    You’re all form buildered up, so you just need
    f.error_message_on(method) (this doesn’t work prior to rails 2)

    Close! I get this error now:

    ActionView::TemplateError (wrong number of arguments (5 for 2)) on
    line #33 of organization_events/new.html.erb:
    30: :size => 30 %>
    31: <%= error_message_on :organization, :church_name %>
    32:
    33:

  • <%= text_field_with_errors( f, :church_name, “Church
    Name”) %>

  • 34:
    35:
  • <%= f.label :address1, “Address:” %>
    36: <%= f.text_field :address1,
    /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/
    

    helpers/form_helper.rb:672:in error_message_on' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_helper.rb:672:inerror_message_on’

    I’m on rails 2.0.2.

  • On 20 Apr 2008, at 19:43, Karl S. wrote:

    Oops, slight change, I was trying to see if I could self. Doesn’t
    really matter, either self or just f, neither works.

    So the problem is with error_message_on. the version you called is
    expecting the first parameter to be the name of an instance variable.
    You’re all form buildered up, so you just need
    f.error_message_on(method) (this doesn’t work prior to rails 2)

    Fred.

    Ahh HA! Yes, I had patched the
    ActionView::Helpers::ActiveRecordHelper / error_message_on method. I
    didn’t like having the error text wrapped in divs, so I changed it to
    spans. I copied the ARH method exactly, only changing the

    to
    , but it looks like that doesn’t allow it being a form_builder
    object anymore.

    But… I fired up ruby-debug and did an inspect on f (that is one
    monster object). Combing through I found that f.object refers to the
    model, so (drum roll please):

    error_message_on( f.object, method)

    Works!

    Simple, fix, and I get to keep my span tags.

    Fred, thanks for sticking through this with me!

    On 20 Apr 2008, at 20:02, Karl S. wrote:

    So the problem is with error_message_on. the version you called is
    expecting the first parameter to be the name of an instance variable.
    You’re all form buildered up, so you just need
    f.error_message_on(method) (this doesn’t work prior to rails 2)

    Close! I get this error now:

    ActionView::TemplateError (wrong number of arguments (5 for 2)) on

    That’s odd - if you get that far it should work. You haven’t got any
    plugins installed tha might be messing with that have you ?

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs