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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Grayson Piercee 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 > > GPIf 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Berci wrote:> Grayson Piercee 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@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 Piercee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Berci wrote: > > Grayson Piercee 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---