# Ror noob need help with easy hidden\_field question

**URL:** <https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820>\
**Category:** Rails\
**Created:** [April 12, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820 "2010-04-12T23:22:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![siouxsioux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siouxsioux](https://www.ruby-forum.com/u/siouxsioux)\
**Post date:** [April 12, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820/1 "2010-04-12T23:22:40Z")

</div>

I’m trying to save users the trouble of creating a username, so I am  
generating it based on their email address. It doesn’t need to be  
unique, it is used in the view only, but that’s not my question.

My question is how do I store it when the user fills out the form?  
Obviously, this won’t work, bc the user’s email isn’t saved yet…any  
help you can provide? The line in \*\*\*\*\*\*\* is the problem. Thank you.

```
- form_for resource_name, resource, :url =>

```

registration\_path(resource\_name) do |f|  
= f.error\_messages

```
    = f.label :email
    = f.text_field :email

    = f.label :password
    = f.password_field :password

    = f.label :password_confirmation
    = f.password_field :password_confirmation

```

\*\*\*\*\*\*\*\*\*\*\*\*\* = f.hidden\_field\_tag(:username,  
@user.email.split(’@’,2).first) \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

```
    = f.submit "Sign Up"
```

---

<div class="post-metadata">

**Author:** ![siouxsioux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siouxsioux](https://www.ruby-forum.com/u/siouxsioux)\
**Post date:** [April 13, 2010, 12:10am UTC](https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820/2 "2010-04-13T00:10:00Z")

</div>

> ```
> = f.error_messages
> 
> ```
> 
> \*\*\*\*\*\*\*\*\*\*\*\*\* = f.hidden\_field\_tag(:username,  
> @user.email.split(’@’,2).first) \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ```
> = f.submit "Sign Up"
> 
> ```

Why do you need to put it into the form? Seems like this would be a  
good spot for a before\_save handler in your user model. Check to see  
if the :username field is blank and if so, set it to the first bit of  
the email address…

-philip

---

<div class="post-metadata">

**Author:** ![siouxsioux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siouxsioux](https://www.ruby-forum.com/u/siouxsioux)\
**Post date:** [April 13, 2010, 2:10am UTC](https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820/3 "2010-04-13T02:10:41Z")

</div>

Philip H. wrote:

> Why do you need to put it into the form? Seems like this would be a  
> good spot for a before\_save handler in your user model. Check to see  
> if the :username field is blank and if so, set it to the first bit of  
> the email address…
> 
> -philip

Thanks! before\_save worked great!

---

<div class="post-metadata">

**Author:** ![siouxsioux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siouxsioux](https://www.ruby-forum.com/u/siouxsioux)\
**Post date:** [April 13, 2010, 2:11am UTC](https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820/4 "2010-04-13T02:11:40Z")

</div>

code for others:

class User \< ActiveRecord::Base  
before\_save :create\_username  
private

def create\_username  
self.username = self.email.split(’@’,2).first  
end

end

---

<div class="post-metadata">

**Author:** ![siouxsioux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siouxsioux](https://www.ruby-forum.com/u/siouxsioux)\
**Post date:** [April 15, 2010, 7:34am UTC](https://www.ruby-forum.com/t/ror-noob-need-help-with-easy-hidden-field-question/186820/5 "2010-04-15T07:34:38Z")

</div>

Sioux Sioux wrote:

> code for others:
> 
> class User \< ActiveRecord::Base  
> before\_save :create\_username  
> private
> 
> def create\_username  
> self.username = self.email.split(‘@’,2).first  
> end
> 
> end

So what happens when removed\_email\_address@domain.invalid and removed\_email\_address@domain.invalid both sign up?  
Perhaps you should just use the whole address.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
