ive got a few models that may or may not have some data: urls / phone numbers / dates / times / etc whats the best way to test and format this data for displaying on a webpage? the way im thinking of doing it would be a few lines of code to: - test if not null - format properly - display in proper div / ul container and then do that for each thing is there a cleaner way to do this? -- 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.
could someone perhaps advise on how to move some of this logic from view to model? like ''get_price_info'' and it returns the properly formatted string or nothing? or should these tests be performed in the view? -- 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.
On Thu, Apr 15, 2010 at 11:05 AM, dan <mr.dan.marks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> could someone perhaps advise on how to move some of this logic from > view to model? > like ''get_price_info'' and it returns the properly formatted string or > nothing? > or should these tests be performed in the view?I''m not sure what you''re looking for here; if you have a view with e.g. <%= @thing.foo %> and this particular "foo" is nil, nothing will display, otherwise you get the value of "foo". If that''s not the behavior you want, please describe in more detail. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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.
if thing.foo is a date, price, url or something else that requires formatting, it requires more than =thing.foo and since you cant format nil, a test needs to be performed before trying to format said data my views are getting littered with these tests and formats, sometimes there nested: is there price data? is there an advance purchase price? is there an advance purchase expiry date? its getting kinda ugly in my view whats the best way to handle this? -- 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.
On Thu, Apr 15, 2010 at 11:36 AM, dan <mr.dan.marks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> if thing.foo is a date, price, url or something else that requires > formatting, it requires more than =thing.foo > and since you cant format nil, a test needs to be performed before > trying to format said data > my views are getting littered with these tests and formats, sometimes > there nested:Some possible approaches: 1) make the default in the database some value other than null. 2) <%= @thing.foo.format rescue nil %> 3) <%= @thing.foo.nil? ? " N/A " : @thing.foo.format %> I''m sure someone will suggest others, but HTH :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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.
The logic for this probably belong in view helpers and not in the model since its really about how the data is displayed and not about how the data behaves. On Apr 15, 8:05 pm, dan <mr.dan.ma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> could someone perhaps advise on how to move some of this logic from > view to model? > like ''get_price_info'' and it returns the properly formatted string or > nothing? > or should these tests be performed in the view?-- 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.
anyone have a good example of how best to move this logic to a helper? -- 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.
It would be easier if you post the actual view code, and then somebody can show you how you might refactor it by moving logic into helpers On Apr 15, 10:12 pm, dan <mr.dan.ma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> anyone have a good example of how best to move this logic to a helper?-- 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.