# H() method in form partial

**URL:** <https://www.ruby-forum.com/t/h-method-in-form-partial/185599>\
**Category:** Rails\
**Created:** [March 23, 2010, 8:46am UTC](https://www.ruby-forum.com/t/h-method-in-form-partial/185599 "2010-03-23T08:46:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 23, 2010, 8:46am UTC](https://www.ruby-forum.com/t/h-method-in-form-partial/185599/1 "2010-03-23T08:46:07Z")

</div>

Hi  
I have a doubt. Dont know it is valid. Just asking. I am trying to  
reuse the same partial \_form for both new and edit. So from new.html.erb  
I call partial like

\<%= render :partial =\> ‘user\_fields’, :locals =\> {:f =\> f}%\>

and in \_user\_fields.html.erb an example field as

\<%= u.label :first\_name,'First Name' %\> \<%= u.text\_field :first\_name,:maxlength =\> 50 %\>

```
  My question is if I call this same partial in edit.html.erb also,

```

how can I apply the h() method like below (since this fields being  
populated with value from db being an edit form)

\<%=h u.label :first\_name,'First Name' %\> \<%=h u.text\_field :first\_name,:maxlength =\> 50 %\>

Thanks in advance  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 23, 2010, 8:55am UTC](https://www.ruby-forum.com/t/h-method-in-form-partial/185599/2 "2010-03-23T08:55:17Z")

</div>

> ```
> My question is if I call this same partial in edit.html.erb also,
> 
> ```
> 
> how can I apply the h() method like below (since this fields being  
> populated with value from db being an edit form)
> 
> \<%=h u.label :first\_name,'First Name' %\> \<%=h u.text\_field :first\_name,:maxlength =\> 50 %\>

I’m pretty sure the text\_field method will cover that for you - you  
wouldn’t want to put the h where you have there or you’d end up escaping  
the actual field.

If you really wanted to override the value then you could do something  
like this:

\<%= u.text\_field :first\_name, :value =\> h(your\_value) %\>

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 23, 2010, 8:56am UTC](https://www.ruby-forum.com/t/h-method-in-form-partial/185599/3 "2010-03-23T08:56:51Z")

</div>

On 23 Mar, 07:46, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> I have a doubt. Dont know it is valid. Just asking. I am trying to  
> reuse the same partial \_form for both new and edit. So from new.html.erb  
> I call partial like

Well it is valid but not useful - The label help would generate  
something like

\<label for='first\_name\>First Name

But then h comes along and escapes the whole lot (not just the label  
contents) - try it for yourself!

Fred
