daily pastebin goal
87%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. How to get values from Ruby class depending on another value
  2. <% (1..@question.qvalues_count.to_i).each do |qvalue| %>
  3.     <% case qvalue
  4.          when 1
  5.           answer_value = @answer.value1
  6.         when 2
  7.           answer_value = @answer.value2
  8.         when 3
  9.           answer_value = @answer.value3
  10.         when 4
  11.           answer_value = @answer.value4
  12.         when 5
  13.           answer_value = @answer.value5
  14.         end %>
  15.        <label for="answer[value<%=qvalue%>]" class="fieldLabel" >Value<%=qvalue%></label>
  16.       <input type="text" id="answer[value<%=qvalue%>]" name="answer[value<%=qvalue%>]"
  17.         value="<%=answer_value%>"
  18.       />
  19.    <% end %>
  20.    
  21. values = {1 => 'my value 1', 2 => 'my value'}
  22. answer_value = values[qvalue]
  23.    
  24. class Answer
  25.   def value(i)
  26.     self.send("value#{i}")
  27.   end
  28. end
  29.    
  30. <% answer_value = eval( '@answer.value' + qvalue.to_s ) %>
  31.    
  32. <% answer_value = @answer.send( 'value'+qvalue.to_s) %>
  33.    
  34. <% answer_value = @answer.send( 'value#{qvalue}' ) %>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top