In one of my views, I have code like this: <% if contact.primary_email != "" %> <%= mail_to contact.primary_email %><br /> <% end %> <% if contact.secondary_email != "" %> <%= mail_to contact.secondary_email %><br /> <% end %> <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> It would fail if, say, primary_email was nil. Should I use partials to improve this code? How?
On 8/28/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In one of my views, I have code like this: > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > %><br /> <% end %> > <% if contact.secondary_email != "" %> <%= mail_to > contact.secondary_email %><br /> <% end %> > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > It would fail if, say, primary_email was nil. > > Should I use partials to improve this code? How?Is contact nil? Should contact actually be @contact? If my understanding is correct, partials are best for keeping re-useable parts of your view that are not too code intensive. For parts of your view that contain alot of coding, then you should use helpers.
Joe Van Dyk wrote:> In one of my views, I have code like this: > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > %><br /> <% end %> > <% if contact.secondary_email != "" %> <%= mail_to > contact.secondary_email %><br /> <% end %> > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > It would fail if, say, primary_email was nil. > > Should I use partials to improve this code? How?Your code wouldn''t fail if an attribute was to be nil. Partials are probably not needed here, but if you wanted to use one you could do something like this: <% for ec in [:primary_email,:secondary_email] @email_contact = contact.send(ec) render(:partial => ''email_contact'') if @email_contact != '''' end %> <%= "Direct: #{contact.phone}<br/>" if contact.phone != '''' %> <%= "Cell: #{contact.cell}" if contact.cell != '''' %> then in _email_contact.rhtml <%= mail_to @email_contact %><br/> -- We develop, watch us RoR, in numbers too big to ignore.
Am Sonntag, den 28.08.2005, 02:23 -0700 schrieb Joe Van Dyk:> In one of my views, I have code like this: > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > %><br /> <% end %> > <% if contact.secondary_email != "" %> <%= mail_to > contact.secondary_email %><br /> <% end %> > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > It would fail if, say, primary_email was nil. > > Should I use partials to improve this code? How?use attribute_present? to check if an attribute has been set. http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000728 <%= if contact.attribute_present? ''primary_email'' %> <%= mail_to contact.primary_email %><br /> <% end %> i think there is no need to use a partial. ciao, norman
Quick answer: Use helpers. Partials are for "bigger" reusable things. ------------------------------------------------------------------------ /*Ronny Hanssen*/ Joe Van Dyk wrote:> In one of my views, I have code like this: > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > %><br /> <% end %> > <% if contact.secondary_email != "" %> <%= mail_to > contact.secondary_email %><br /> <% end %> > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > It would fail if, say, primary_email was nil. > > Should I use partials to improve this code? How? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 8/28/05, James Earl <jamesd.earl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/28/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > In one of my views, I have code like this: > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > %><br /> <% end %> > > <% if contact.secondary_email != "" %> <%= mail_to > > contact.secondary_email %><br /> <% end %> > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > It would fail if, say, primary_email was nil. > > > > Should I use partials to improve this code? How? > > Is contact nil? Should contact actually be @contact? > > If my understanding is correct, partials are best for keeping > re-useable parts of your view that are not too code intensive. For > parts of your view that contain alot of coding, then you should use >contact is not nil, and it should be contact, not @contact, as it''s in a helper. That code above was in the "contact" partial. I called it via <%render_partial_collection "contact", @contacts %> So, should that code go inside a helper, where I check to see if the primary_email is nil, and then check to see if it''s not empty, and then display it? (I''m running into this via unit tests, btw. My fixtures don''t have all the fields defined. In the actual application, this doesn''t seem to be a problem, since the fields always have at least "" in them.
On 8/28/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> Joe Van Dyk wrote: > > In one of my views, I have code like this: > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > %><br /> <% end %> > > <% if contact.secondary_email != "" %> <%= mail_to > > contact.secondary_email %><br /> <% end %> > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > It would fail if, say, primary_email was nil. > > > > Should I use partials to improve this code? How? > > Your code wouldn''t fail if an attribute was to be nil.It does fail. if contact.primary_email is nil, then trying to do: contact.primary_email != "" will throw an exception.> Partials are probably not needed here, but if you wanted to > use one you could do something like this: > > <% for ec in [:primary_email,:secondary_email] > @email_contact = contact.send(ec) > render(:partial => ''email_contact'') if @email_contact != '''' > end > %> > <%= "Direct: #{contact.phone}<br/>" if contact.phone != '''' %> > <%= "Cell: #{contact.cell}" if contact.cell != '''' %> > > then in _email_contact.rhtml > > <%= mail_to @email_contact %><br/> > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
This would actually fail, because nil != "". If you want to get creative: render_contact(contact, :primary_email => :mail, :secondary_email => :mail, :phone => "Direct", :cell => "Cell") def render_contact(contact, *desc) output = "" Hash[desc*].each do |field, rendering| unless contact.send(field).nil? || contact.send(field).length.zero? output .if rendering == :mail mail_to contact.send(field) else "#{rendering}: #{contact.send(field)}" end + "<br />" end end end (untested, but you should be able to fix it up if it doesn''t work) - Chris On 8/28/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > Joe Van Dyk wrote: > > In one of my views, I have code like this: > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > %><br /> <% end %> > > <% if contact.secondary_email != "" %> <%= mail_to > > contact.secondary_email %><br /> <% end %> > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end > %> > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > It would fail if, say, primary_email was nil. > > > > Should I use partials to improve this code? How? > > Your code wouldn''t fail if an attribute was to be nil. > > Partials are probably not needed here, but if you wanted to > use one you could do something like this: > > <% for ec in [:primary_email,:secondary_email] > @email_contact = contact.send(ec) > render(:partial => ''email_contact'') if @email_contact != '''' > end > %> > <%= "Direct: #{contact.phone}<br/>" if contact.phone != '''' %> > <%= "Cell: #{contact.cell}" if contact.cell != '''' %> > > then in _email_contact.rhtml > > <%= mail_to @email_contact %><br/> > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Mark Reginald James wrote:> then in _email_contact.rhtml > > <%= mail_to @email_contact %><br/>^ |__ delete the @ -- We develop, watch us RoR, in numbers too big to ignore.
On 8/29/05, Norman Timmler <norman-QkIQCVqxERM@public.gmane.org> wrote:> Am Sonntag, den 28.08.2005, 02:23 -0700 schrieb Joe Van Dyk: > > In one of my views, I have code like this: > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > %><br /> <% end %> > > <% if contact.secondary_email != "" %> <%= mail_to > > contact.secondary_email %><br /> <% end %> > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > It would fail if, say, primary_email was nil. > > > > Should I use partials to improve this code? How? > > use attribute_present? to check if an attribute has been set. > > http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000728 > > <%= if contact.attribute_present? ''primary_email'' %> > <%= mail_to contact.primary_email %><br /> > <% end %>Isn''t that what partials are for? You mean, it''s better to do something like: (in list.rhtml) <table> <% for contact in @contacts %> <tr> <td> <b><%= contact.name %><br /></b> <i><%= contact.title %></i> </td> <td></td> <td valign="top"> <% if contact.primary_email != "" %> <%= mail_to contact.primary_email %><br /> <% end %> <% if contact.secondary_email != "" %> <%= mail_to contact.secondary_email %><br /> <% end %> <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> </td> <td> <%= render :partial => "shared/admin_links", :object => contact, :locals => { :options => [:delete_option] } %> </td> </tr> <% end %> </table> As opposed to what I''m doing now: (in list.rhtml) <table> <%= render_partial_collection "contact", @contacts %> </table> (in _contact.rhtml) <tr> <td> <b><%= contact.name %><br /></b> <i><%= contact.title %></i> </td> <td width="10%"></td> <td> <% if contact.primary_email != "" %> <%= mail_to contact.primary_email %><br /> <% end %> <% if contact.secondary_email != "" %> <%= mail_to contact.secondary_email %><br /> <% end %> <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> </td> <td> <%= render :partial => "shared/admin_links", :object => contact, :locals => { :options => [:delete_option] } %> </td> </tr>
On 8/29/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/28/05, James Earl <jamesd.earl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 8/28/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > In one of my views, I have code like this: > > > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > > %><br /> <% end %> > > > <% if contact.secondary_email != "" %> <%= mail_to > > > contact.secondary_email %><br /> <% end %> > > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > > > It would fail if, say, primary_email was nil. > > > > > > Should I use partials to improve this code? How? > > > > Is contact nil? Should contact actually be @contact? > > > > If my understanding is correct, partials are best for keeping > > re-useable parts of your view that are not too code intensive. For > > parts of your view that contain alot of coding, then you should use > > > > contact is not nil, and it should be contact, not @contact, as it''s in a helper. > > That code above was in the "contact" partial. I called it via <%> render_partial_collection "contact", @contacts %> > > So, should that code go inside a helper, where I check to see if the > primary_email is nil, and then check to see if it''s not empty, and > then display it? > > (I''m running into this via unit tests, btw. My fixtures don''t have > all the fields defined. In the actual application, this doesn''t seem > to be a problem, since the fields always have at least "" in them.When you start to feel like you''re about to go crazy with the number of <% %> and <%= %> that you''re typing... you know it''s time to use helpers or rethink some stuff! :)
On 8/29/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/28/05, James Earl <jamesd.earl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 8/28/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > In one of my views, I have code like this: > > > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > > %><br /> <% end %> > > > <% if contact.secondary_email != "" %> <%= mail_to > > > contact.secondary_email %><br /> <% end %> > > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > > > It would fail if, say, primary_email was nil. > > > > > > Should I use partials to improve this code? How? > > > > Is contact nil? Should contact actually be @contact? > > > > If my understanding is correct, partials are best for keeping > > re-useable parts of your view that are not too code intensive. For > > parts of your view that contain alot of coding, then you should use > > > > contact is not nil, and it should be contact, not @contact, as it''s in a helper. > > That code above was in the "contact" partial. I called it via <%> render_partial_collection "contact", @contacts %> > > So, should that code go inside a helper, where I check to see if the > primary_email is nil, and then check to see if it''s not empty, and > then display it?I may not fully understand the problem, but you should be able to just do: <% if !contact.primary_email.nil? %><%= mail_to contact.primary_email %><% end %> ...shouldn''t you?
Joe Van Dyk wrote:>>Your code wouldn''t fail if an attribute was to be nil. > > It does fail. > > if contact.primary_email is nil, then trying to do: > contact.primary_email != "" > will throw an exception.Will it? Type this into ruby: a=nil puts(''123'') if a != '''' Of course it would fail if contact itself is nil. -- We develop, watch us RoR, in numbers too big to ignore.
Chris Lambert wrote:> This would actually fail, because nil != "". If you want to get creative:Right, sorry. The test doesn''t throw an exception, but is true when it shouldn''t be. -- We develop, watch us RoR, in numbers too big to ignore.
On 8/30/05, James Earl <jamesd.earl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/29/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 8/28/05, James Earl <jamesd.earl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 8/28/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > In one of my views, I have code like this: > > > > > > > > <% if contact.primary_email != "" %> <%= mail_to contact.primary_email > > > > %><br /> <% end %> > > > > <% if contact.secondary_email != "" %> <%= mail_to > > > > contact.secondary_email %><br /> <% end %> > > > > <% if contact.phone != "" %> Direct: <%= contact.phone %><br /> <% end %> > > > > <% if contact.cell != "" %> Cell: <%= contact.cell %> <% end %> > > > > > > > > It would fail if, say, primary_email was nil. > > > > > > > > Should I use partials to improve this code? How? > > > > > > Is contact nil? Should contact actually be @contact? > > > > > > If my understanding is correct, partials are best for keeping > > > re-useable parts of your view that are not too code intensive. For > > > parts of your view that contain alot of coding, then you should use > > > > > > > contact is not nil, and it should be contact, not @contact, as it''s in a helper. > > > > That code above was in the "contact" partial. I called it via <%> > render_partial_collection "contact", @contacts %> > > > > So, should that code go inside a helper, where I check to see if the > > primary_email is nil, and then check to see if it''s not empty, and > > then display it? > > I may not fully understand the problem, but you should be able to just do: > > <% if !contact.primary_email.nil? %><%= mail_to contact.primary_email > %><% end %> > > ...shouldn''t you?I also don''t want to display the email/phone/etc if it''s an empty string as well.
On 8/30/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> Joe Van Dyk wrote: > > >>Your code wouldn''t fail if an attribute was to be nil. > > > > It does fail. > > > > if contact.primary_email is nil, then trying to do: > > contact.primary_email != "" > > will throw an exception. > > Will it? Type this into ruby: > > a=nil > puts(''123'') if a != '''' > > Of course it would fail if contact itself is nil.Whoops, you''re right. The problem is that it returns true. So, the email/phone information gets displayed if its nil.