SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email tag and image problem

    How do you show an email image with an address attached to it. I have tried numerous mail_to and image_tag variations but just can't seem to figure it out. Does anyone have the solution to this problem?

    Rick

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Mexico City
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    link_to image_tag(parameters...), 'mailto:someone@somewhere.com'

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ivan,
    Thanks for that, so simple yet so difficult at the same time. Syntax is the big problem here and getting used to it.

    Rick

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Mexico City
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I feel your pain But rest assured you'll get up to speed fast enough, and then there's no turning back.

    Code:
    <%= link_to image_tag("emailButton"), "mailto:#{list_stripes.email}" %>
    See the difference? Let me try to explain:

    image_tag("emailButton") translates to this: <img src="emailButton.png" />

    The first parameter to link_to, would be what goes between <a href...> and </a>, and the second parameter is the actual href. So, it outputs this (obviosly with the correct values):

    <a href="mailto:whatever is the value of list_stripes.email">here goes the output of the image_tag function (a helper actually)</a>

    Also, if you don't know what #{..} is for inside a double quoted string ("..."), it works by replacing that brackets along with anything inside them with the value of the ruby expression inside, in this case, the value of list_stripes.email

    I hope this helps you better understand how it all works.

    Ivan V.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are a PHP programmer, #{var} is the same as $var in PHP in a string:

    $var = "a string";
    $one = "this is $var";
    $two = 'this is $var';

    Now, $one is "this is a string", and $two is "this is $var" because the variable substitution doesn't exist in single-quoted strings.

    This is the same in Ruby:

    var = "a string"
    one = "this is #{var}"
    two = 'this is #{var}'

    But you can put "real" code in it too:

    three = "two is #{two.kind_of?(String) ? 'a string' : 'not a string'}"

    three will be: "two is a string".

    I hope this is not confusing you :P.

  6. #6
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me thank you both for those excellent examples and descriptions. I am awaiting the book but in the meantime I am trying to forge on. I have one more relatively simple question. Let's say a returned field is null or blank. Is there an easy way to test for that condition. I am using
    Code:
    <% if list_stripes.email != '' %>
    as an example and I have tried
    Code:
    <% if list_stripes.email.nil? %>
    with mixed success. Which method do you recommend.

    Rick

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Mexico City
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use both:
    Code:
    <% if list_stripes.email.nil? || list_stripes.email.empty? %>

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, just use object.blank?

    Code:
    >> [].blank?
    => true
    >> [1, 2, 3].blank?
    => false
    >> "".blank?
    => true
    >> 0.blank?
    => false
    >> nil.blank?
    => true
    Works for many things, but keep in mind that you have to use 0.zero? to test whether a number is zero or not.

  9. #9
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that info. Where is it documented in the API docs?

    Rick

  10. #10
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    No, just use object.blank?

    Code:
    >> [].blank?
    => true
    >> [1, 2, 3].blank?
    => false
    >> "".blank?
    => true
    >> 0.blank?
    => false
    >> nil.blank?
    => true
    Works for many things, but keep in mind that you have to use 0.zero? to test whether a number is zero or not.
    What version of Ruby is that? I can't find it in the online API docs or the Rails docs if its an extension.

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Mexico City
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice, I didn't know about those two.

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not in the Ruby documentation, it works only if you use Rails.

    They've nodoc'd the method:

    Code:
    class Object #:nodoc:
      # "", "   ", nil, [], and {} are blank
      def blank?
        if respond_to?(:empty?) && respond_to?(:strip)
          empty? or strip.empty?
        elsif respond_to?(:empty?)
          empty?
        else
          !self
        end
      end
    end
    
    class NilClass #:nodoc:
      def blank?
        true
      end
    end
    
    class FalseClass #:nodoc:
      def blank?
        true
      end
    end
    
    class TrueClass #:nodoc:
      def blank?
        false
      end
    end
    
    class Array #:nodoc:
      alias_method :blank?, :empty?
    end
    
    class Hash #:nodoc:
      alias_method :blank?, :empty?
    end
    
    class String #:nodoc:
      def blank?
        empty? || strip.empty?
      end
    end
    
    class Numeric #:nodoc:
      def blank?
        false
      end
    end
    It's in rails/activesupport/lib/active_support/core_ext/blank.rb.

    Maybe it's intended for internal use only? I don't remember how I learned that this method exists...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •