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…
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…