SitePoint Sponsor

User Tag List

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

    Retrieving String Value from method

    I have a method that I use to create simple drop-down boxes. On bit of code from that method is:
    Code:
    COMMITMENT = [
    		[ "Very Interested", 1 ],
    		[ "Somewhat Interested", 2 ],
    		[ "Interested", 3 ],
    		[ "Not Very Interested", 4 ],
    		[ "Uninterested", 5 ]
    		].freeze
    The integer value is stored in the db. I want to show the string value in a view page. Is there some syntax that will allow me to do that - something like
    Code:
    <%= method:COMMITTMENT.commitment_id %>
    where commitment_id=1, would give me "Very Interested".

    Rick
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried using an array rather than a hash. In an array, each element will be automatically indexed:

    http://whytheluckystiff.net/ruby/pic...f_c_array.html

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <%= COMMITMENT.detect{|i| i[1] == commitment_id}[0] %>
    This code detects the option where i[1] (the second element in the array, so that's the number) is the same as commitment_id Then it prints the first element of the detected commitment (...id}[0]).

    But I think you should do this:
    Code:
    COMMITMENT= ["Very Interested",
    		"Somewhat Interested",
    		"Interested",
    		"Not Very Interested",
    		"Uninterested"]
    and:

    Code:
    <%= COMMITMENT[commitment_id] %>

  4. #4
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestions. I took what you provided and tried some experimentation and here is what works also:
    Code:
    <%= Dropdown::COMMITMENT[k.commitment][0] %>
    which is similar to the last post and still fulfils the requirement.

    Thanks
    Ruby, Ruby when will you be mine


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
  •