New method initializing a property

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

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

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

@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. [email protected]