0

I am trying to create a form using the following view file

<%= form_for :spec do |form| %>
<fieldset>
<legend><%= @title %></legend>

<%= text_field_for form, "first_name" %>
<%= text_field_for form, "last_name" %>
<div class="form_row">
<label for="gender">Gender: </label>
<%= radio_button :spec, :gender, "Male" %> Male
<%= radio_button :spec, :gender, "Female" %> Female
<%= radio_button :spec, :gender, "Other" %> Other
</div>
<div class="form_row">
<label for="birthdate">Birthdate:</label>
<%= date_select :spec, :birthdate,
            :start_year => Spec::START_YEAR,
            :end_year => Time.now.year,
            :include_blank => true,
            :order => [:month,:day,:year] %>
</div>
<%= text_field_for form, "occupation" %>
<%= text_field_for form, "city" %>
<%= text_field_for form, "state" %>
<%= text_field_for form, "zip_code", Spec::ZIP_CODE_LENGTH %>

<%= submit_tag "Update", :class => "submit" %>
</fieldset>


<%end%>

And I am using the following helper method

def text_field_for (form, field, 
            size=HTML_TEXT_FIELD_SIZE, 
            maxlength=DB_STRING_MAX_LENGTH)
    label = content_tag("label","#{field.humanize}:", :for => field)
    form_field = form.text_field field, :size => size, :maxlength => maxlength
    content_tag("div", "#{label} #{form_field}", :class => "form_row")
end

However, my output gives HTML code for

<label for="first_name">First name:</label> <input id="spec_first_name" maxlength="255" name="spec[first_name]" size="15" type="text" />
<label for="last_name">Last name:</label> <input id="spec_last_name" maxlength="255" name="spec[last_name]" size="15" type="text" />

instead of textfields and labels

Can anyone suggest a solution for this?

1 Answer 1

1

Update text_field_for method as below:

def text_field_for (form, field, 
            size=HTML_TEXT_FIELD_SIZE, 
            maxlength=DB_STRING_MAX_LENGTH)
    label = content_tag("label","#{field.humanize}:", :for => field)
    form_field = form.text_field field, :size => size, :maxlength => maxlength
    content_tag "div", label + form_field,  :class => "form_row" 
end

You were treating the label and form_field as String. You shouldn't be interpolating them.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you copy it as it is? It works with both as a block as well as the current edited answer.
I updated the helper into the controller specific helper file instead on common application helper. It worked! thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.