3

Based on feedback, I am revising this question. How do i convert an integer array to a set of values displayed on the view page using the Constants (defined in the model). I can do it on my form page but have not figured it out for the Index.

On an Index Page (if dbase has grades: [0, 1, 2], the page should display as A+, A, B)

something like what is done for days of the week (e.g. http://hightechsorcery.com/2010/02/16/ruby-arrays-and-hashes-and-days-of-the-week/

....
<h4 class="h3"><%= @gradestemp %>

CONTROLLER Labels Controller

  def index
    @labels = current_user.labels
    grad = []
    @gradestemp = Contact::GRADES.each_with_index { |x, i| grad << [x, i] }
    render
  end

MODEL NB: GRADES is a constant - I am trying to also use in Labels class Label < ActiveRecord::Base NB: this is in the CONTACT model

  GRADES  = [["A+",0 ], ["A",1], ["B", 2], [ "C",3], [ "D",4], [ "-",5]]

Am i able to access the Contact GRADES while in the Labels controller?

i have found this SO - which is similar to what i am trying to do: Ruby: How to store and display a day of the week?

Based suggestion below, this did the trick:

<h4 class="h3"><%= print_campaign.grades.compact.map{|idx| Contact::GRADES[idx][0]}.join(' ')  %>
8
  • (1) Do you have your controller methods (think CRUD) properly setup? (2) Why is your Label model empty? It should have an association with your contacts. (3) Please narrow your post to one question. (4) Edit your post (with the previous details, not as comments) and correctly format the code. Paste your code directly in, then click the {} button, to format it. I tried editing it for format, but your post was too jumbled with details for me to handle Commented Aug 26, 2015 at 19:00
  • hi - thanks for feedback - i updated the question. Yes, I was thinking my issue was that i was not saving the array properly, but maybe its more of a front end (of how do i take the array (from labels) and render back to display the one Contact::GRADES selected. Commented Aug 26, 2015 at 19:27
  • I would suggest that you post any error messages you have. How do you know it is not saving correctly? Does it display right in the database, but not in the view? Commented Aug 26, 2015 at 19:38
  • Okay -see above - on the form input, i replaced the Contact::GRADES to be explicit and this now works so when go via EDIT, it displays the collection I had picked ( A+, A, B). So why doesn't referencing the Contact::GRADES work on EDIT? Commented Aug 26, 2015 at 19:54
  • 1
    Not very clear, but if you just want to convert array like [1,2,3] why not just do [1,2,3].map{|idx| grade[idx]}.join(' ') Commented Aug 27, 2015 at 17:48

1 Answer 1

1

[0, 1, 2].map {|g| Contact::GRADES.select.map {|letter, val| val == g; letter}

Really though GRADES seems much better off as a Hash:

GRADES = { "A+" => 0, "A" => 1, .. }

Then you lookup would be much simpler

[0, 1, 2].map {|g| Contact::GRADES.key(g) }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.