# New method initializing a property

**URL:** <https://www.ruby-forum.com/t/new-method-initializing-a-property/129259>\
**Category:** Rails\
**Created:** [February 15, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/new-method-initializing-a-property/129259 "2008-02-15T20:40:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [February 15, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/new-method-initializing-a-property/129259/1 "2008-02-15T20:40:17Z")

</div>

Hello,

Something really simple doesn’t seem to be happening for me.

I have a class called User, I’d like to set a property called “role” at  
the time the new form is called. In users\_controller I have the  
following

def new  
@user = new User  
@user.role = “Non-Admin”  
end

by the time the form posts to the create method, the @user.role is nil.  
I tried passing it through via a hidden\_field\_tag like this:

hidden\_field\_tag :role, @user.role

but that doesn’t seem to worth either.

TIA

GP

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [February 15, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/new-method-initializing-a-property/129259/2 "2008-02-15T23:32:45Z")

</div>

Grayson P. wrote:

> Hello,
> 
> Something really simple doesn’t seem to be happening for me.
> 
> I have a class called User, I’d like to set a property called “role” at  
> the time the new form is called. In users\_controller I have the  
> following
> 
> def new  
> @user = new User  
> @user.role = “Non-Admin”  
> end
> 
> by the time the form posts to the create method, the @user.role is nil.  
> I tried passing it through via a hidden\_field\_tag like this:
> 
> hidden\_field\_tag :role, @user.role
> 
> but that doesn’t seem to worth either.
> 
> TIA
> 
> GP

If that is what you have above, the @user = new User won’t work…  
it should read as:  
@user = User.new(:role =\> “Non-Admin”)

when the “new” view sends the request back to your controller via the  
:create action, there is no object representing the “user”, just a bunch  
of entries in param hash… (probably param[:user]), you must make sure  
that the :role is transferred along with the rest of the params.

you can easily verify this by sticking a debugger statement at the top  
of your :create action and checking the params yourself to make sure you  
resend it back to your controller…

hth

ilan

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [February 16, 2008, 3:58am UTC](https://www.ruby-forum.com/t/new-method-initializing-a-property/129259/3 "2008-02-16T03:58:22Z")

</div>

Ilan,

Thank you very much for replying. I understand better how to  
instantiate the object now, however just that didn’t seem to fix my  
problem. I’m guessing from your reply that you’re still recommending  
hidden fields to hold the value. My concern is that someone can  
manipulate HTML and change the hidden value to gain access.

Thanks,

GP

Ilan B. wrote:

> Grayson P. wrote:
> 
> > Hello,
> > 
> > Something really simple doesn’t seem to be happening for me.
> > 
> > I have a class called User, I’d like to set a property called “role” at  
> > the time the new form is called. In users\_controller I have the  
> > following
> > 
> > def new  
> > @user = new User  
> > @user.role = “Non-Admin”  
> > end
> > 
> > by the time the form posts to the create method, the @user.role is nil.  
> > I tried passing it through via a hidden\_field\_tag like this:
> > 
> > hidden\_field\_tag :role, @user.role
> > 
> > but that doesn’t seem to worth either.
> > 
> > TIA
> > 
> > GP
> 
> If that is what you have above, the @user = new User won’t work…  
> it should read as:  
> @user = User.new(:role =\> “Non-Admin”)
> 
> when the “new” view sends the request back to your controller via the  
> :create action, there is no object representing the “user”, just a bunch  
> of entries in param hash… (probably param[:user]), you must make sure  
> that the :role is transferred along with the rest of the params.
> 
> you can easily verify this by sticking a debugger statement at the top  
> of your :create action and checking the params yourself to make sure you  
> resend it back to your controller…
> 
> hth
> 
> ilan

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [February 16, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/new-method-initializing-a-property/129259/4 "2008-02-16T18:55:17Z")

</div>

@GP – both your solution and Ilan’s should have the same net result,  
though Ilan’s is the preferred “Rail’s Way” of doing things.

I suspect that the problem lies in your view. If you post that we  
might be able to offer more help.

On Feb 15, 9:58 pm, Grayson P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
