views:

129

answers:

2

hi all,

how do i access hidden value fields from the controller class.

my hidden value field is

<input id="user_id" name="user.id" size="30" type="text" value="<%= @user.id %>" />

currently i am trying to access with @user.id , @user = User.find(@user.id) but its generating error like

" Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id "

then how do i access the above hidden value

thank in advance, Mahesh

+1  A: 

You need to change the name of your field to user_id to be more traditional.

You access any POST/GET data with the params hash.

# your controller
params[:user_id]
# => "field_value
Simone Carletti
+1  A: 

Two quick notes. I'd avoid depending on the user_id as submitted by a form, it's easy to forge. It's better to pull it from your authentication in the controller create, update, or delete method.

Second, I'd look into form helpers, such as hidden_field within a form_for or a fields_for.

<% form_form @some_model do %>
   <%= f.hidden_field(:some_field_on_the_model) %>
   <%= hidden_field(:some_model, :some_field) %>

Then you use the previously detailed params hash to pull out the values back in the controller.

mymodel = MyModel.find(params[:some_field])
MattMcKnight