Hi, I want to be able to have a more detailed friendly message for users when they register with an email address thats already taken. At the moment I have written a small validation method to produce what I want, personally I dont like it because I''ve had to include modules that shouldn''t really be there (ActionController::UrlWriter and ActionView::Helpers) I was considering extending the error_message_for method to check for error messages on fields against a list stored in a constant then replacing them in the errors array, but I dont really like that either. Does anyone know of a plugin or any suggestions on how I could make this cleaner? Here''s what I have so far. <pre> class User < ActiveRecord::Base include ActionController::UrlWriter include ActionView::Helpers validate :unique_email_address ... def unique_email_address if self.class.find_by_email(email) errors.add(:email, "The email address #{email} has already been used. Please use a different email address or #{link_to(''Sign in'',login_path)}.") end end ... end </pre> Thanks --Rob -- 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.
Why not just use validates_uniqueness_of, with a custom :message ? custom-err-msg is handy for custom messages: http://github.com/gumayunov/custom-err-msg I know that doesn''t answer your URL issue, but it would seem a simpler/ tidier solution even if you still wanted to include the link. I prefer to just leave navigation out of notices. On Jan 29, 9:55 am, Rob Aldred <rald...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I want to be able to have a more detailed friendly message for users > when they register with an email address thats already taken. > > At the moment I have written a small validation method to produce what > I want, personally I dont like it because I''ve had to include modules > that shouldn''t really be there (ActionController::UrlWriter and > ActionView::Helpers) > > I was considering extending the error_message_for method to check for > error messages on fields against a list stored in a constant then > replacing them in the errors array, but I dont really like that > either. > > Does anyone know of a plugin or any suggestions on how I could make > this cleaner? > Here''s what I have so far. > > <pre> > class User < ActiveRecord::Base > include ActionController::UrlWriter > include ActionView::Helpers > > validate :unique_email_address > ... > > def unique_email_address > if self.class.find_by_email(email) > errors.add(:email, "The email address #{email} has already > been used. Please use a different email address or #{link_to(''Sign > in'',login_path)}.") > end > end > ... > end > > </pre> > > Thanks > --Rob-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-29 13:24 UTC
Re: Writing more advanced activerecord error messages
Rob Aldred wrote:> Hi, > I want to be able to have a more detailed friendly message for users > when they register with an email address thats already taken. > > At the moment I have written a small validation method to produce what > I want, personally I dont like it because I''ve had to include modules > that shouldn''t really be there (ActionController::UrlWriter and > ActionView::Helpers)If you need a URL in your error message, then this is the proper way, I think. It''s not true that UrlWriter "shouldn''t really be there" if it''s necessary for the functionality you need. Of course, to some extent this is a design flaw of Rails: validations should certainly be handled by the model, but I find myself thinking that error messages should have been in the controller... 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.
Keavy, Indeed, it would be simpler, but messages with an actions provide a much better way to persuade customers into doing certain actions and in turn providing them with a better experience, rather than them leaving the site because something didn''t work. Marnen, I also find myself thinking that, if the consensus is that including the necessary modules is OK even if their functionality isn''t usually meant for models, then I''m happy to leave it be :) Thanks for your input. --Rob On Jan 29, 12:07 pm, keavy <keavy.mcm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Why not just use validates_uniqueness_of, with a custom :message ? > > custom-err-msg is handy for custom messages:http://github.com/gumayunov/custom-err-msg > > I know that doesn''t answer your URL issue, but it would seem a simpler/ > tidier solution even if you still wanted to include the link. I prefer > to just leave navigation out of notices. > > On Jan 29, 9:55 am, Rob Aldred <rald...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > I want to be able to have a more detailed friendly message for users > > when they register with an email address thats already taken. > > > At the moment I have written a small validation method to produce what > > I want, personally I dont like it because I''ve had to include modules > > that shouldn''t really be there (ActionController::UrlWriter and > > ActionView::Helpers) > > > I was considering extending the error_message_for method to check for > > error messages on fields against a list stored in a constant then > > replacing them in the errors array, but I dont really like that > > either. > > > Does anyone know of a plugin or any suggestions on how I could make > > this cleaner? > > Here''s what I have so far. > > > <pre> > > class User < ActiveRecord::Base > > include ActionController::UrlWriter > > include ActionView::Helpers > > > validate :unique_email_address > > ... > > > def unique_email_address > > if self.class.find_by_email(email) > > errors.add(:email, "The email address #{email} has already > > been used. Please use a different email address or #{link_to(''Sign > > in'',login_path)}.") > > end > > end > > ... > > end > > > </pre> > > > Thanks > > --Rob > >-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Although, using a Proc for the :message using that plugin could be an option, ill test it out and see if i can include links in the string returned by the Proc. Thanks again --Rob On Jan 29, 12:07 pm, keavy <keavy.mcm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Why not just use validates_uniqueness_of, with a custom :message ? > > custom-err-msg is handy for custom messages:http://github.com/gumayunov/custom-err-msg > > I know that doesn''t answer your URL issue, but it would seem a simpler/ > tidier solution even if you still wanted to include the link. I prefer > to just leave navigation out of notices. > > On Jan 29, 9:55 am, Rob Aldred <rald...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > I want to be able to have a more detailed friendly message for users > > when they register with an email address thats already taken. > > > At the moment I have written a small validation method to produce what > > I want, personally I dont like it because I''ve had to include modules > > that shouldn''t really be there (ActionController::UrlWriter and > > ActionView::Helpers) > > > I was considering extending the error_message_for method to check for > > error messages on fields against a list stored in a constant then > > replacing them in the errors array, but I dont really like that > > either. > > > Does anyone know of a plugin or any suggestions on how I could make > > this cleaner? > > Here''s what I have so far. > > > <pre> > > class User < ActiveRecord::Base > > include ActionController::UrlWriter > > include ActionView::Helpers > > > validate :unique_email_address > > ... > > > def unique_email_address > > if self.class.find_by_email(email) > > errors.add(:email, "The email address #{email} has already > > been used. Please use a different email address or #{link_to(''Sign > > in'',login_path)}.") > > end > > end > > ... > > end > > > </pre> > > > Thanks > > --Rob > >-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-29 14:35 UTC
Re: Writing more advanced activerecord error messages
Rob Aldred wrote:> Although, using a Proc for the :message using that plugin could be an > option, ill test it out and see if i can include links in the string > returned by the Proc.I don''t see why not. It''s just a string.> > Thanks again > --RobBest, -- 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.
Just to update, still struggling with this. It seems now I''m being caught about using both ''login_path'' and ''link_to'' because both use different custom url_for methods `login_path` from `ActionController::UrlWriter` and `link_to` from `ActionView::Helpers::UrlHelper` Seems to be some sort of conflict with both these libraries. Might have to scrap this whole idea and write markdown or something in my messages and replace the error_message_for method to generate the HTML --Rob On Jan 29, 2:35 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Rob Aldred wrote: > > Although, using a Proc for the :message using that plugin could be an > > option, ill test it out and see if i can include links in the string > > returned by the Proc. > > I don''t see why not. It''s just a string. > > > > > Thanks again > > --Rob > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.