I''m trying to save users the trouble of creating a username, so I am generating it based on their email address. It doesn''t need to be unique, it is used in the view only, but that''s not my question. My question is how do I store it when the user fills out the form? Obviously, this won''t work, bc the user''s email isn''t saved yet...any help you can provide? The line in ******* is the problem. Thank you. - form_for resource_name, resource, :url => registration_path(resource_name) do |f| = f.error_messages = f.label :email = f.text_field :email = f.label :password = f.password_field :password = f.label :password_confirmation = f.password_field :password_confirmation ************* = f.hidden_field_tag(:username, @user.email.split(''@'',2).first) *********************** = f.submit "Sign Up" -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2010-Apr-12 22:08 UTC
Re: ror noob need help with easy hidden_field question
> I''m trying to save users the trouble of creating a username, so I am > generating it based on their email address. It doesn''t need to be > unique, it is used in the view only, but that''s not my question. > > My question is how do I store it when the user fills out the form? > Obviously, this won''t work, bc the user''s email isn''t saved yet...any > help you can provide? The line in ******* is the problem. Thank you. > > - form_for resource_name, resource, :url => > registration_path(resource_name) do |f| > = f.error_messages > > = f.label :email > = f.text_field :email > > = f.label :password > = f.password_field :password > > = f.label :password_confirmation > = f.password_field :password_confirmation > > ************* = f.hidden_field_tag(:username, > @user.email.split(''@'',2).first) *********************** > > = f.submit "Sign Up"Why do you need to put it into the form? Seems like this would be a good spot for a before_save handler in your user model. Check to see if the :username field is blank and if so, set it to the first bit of the email address... -philip -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom wrote:> Why do you need to put it into the form? Seems like this would be a > good spot for a before_save handler in your user model. Check to see > if the :username field is blank and if so, set it to the first bit of > the email address... > > -philipThanks! before_save worked great! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
code for others: class User < ActiveRecord::Base before_save :create_username private def create_username self.username = self.email.split(''@'',2).first end end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Apr-15 05:34 UTC
Re: ror noob need help with easy hidden_field question
Sioux Sioux wrote:> code for others: > > class User < ActiveRecord::Base > before_save :create_username > private > > def create_username > self.username = self.email.split(''@'',2).first > end > > endSo what happens when jim-YDxpq3io04c@public.gmane.org and jim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org both sign up? Perhaps you should just use the whole address. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.