Guest User

Untitled

a guest
Feb 12th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. ## Edit view
  2.  
  3. <% form_for @rka_completion do |f| %>
  4. <%= f.error_messages%>
  5.  
  6.  
  7. <br/>
  8.  
  9.  
  10. <% f.fields_for :rka_answers do |answer_form| %>
  11. <%= answer_form.hidden_field "answer", :value => 0 %>
  12. <%= answer_form.hidden_field "question_id" %>
  13. <table>
  14. <tr>
  15. <td class="td-completion"><%= answer_form.radio_button("answer", "1", :class => 'displayblock') %></td>
  16. <td class="td-completion"><%= answer_form.radio_button("answer", "2", :class => 'displayblock') %></td>
  17. <td class="td-completion"><%= answer_form.radio_button("answer", "3", :class => 'displayblock') %></td>
  18. <td class="td-completion"><%= answer_form.radio_button("answer", "4", :class => 'displayblock') %></td>
  19. <td class="td-completion"><%= answer_form.radio_button("answer", "5", :class => 'displayblock') %></td>
  20. </tr>
  21.  
  22. <tr>
  23. <td class="td-completion">Begynder</td>
  24. <td class="td-completion"></td>
  25. <td class="td-completion">Kompetent</td>
  26. <td class="td-completion"></td>
  27. <td class="td-completion">Ekspert</td>
  28. </tr>
  29. </table>
  30.  
  31. <br/>
  32. <% end %> <!-- fields for end-->
  33.  
  34. <br/>
  35.  
  36.  
  37.  
  38. <%= f.submit%>
  39. <% end %> <!-- end of form -->
  40.  
  41.  
  42. ## Rka_answer model
  43. class RkaAnswer < ActiveRecord::Base
  44. validate :answer_valid_answer_integer
  45. has_one :rka_question
  46. belongs_to :rka_completion
  47.  
  48. protected
  49. def answer_valid_answer_integer
  50. return if (1..5).include?(answer)
  51. q = RkaQuestion.find question_id
  52. #symbol_field = question_id.to_sym
  53.  
  54. errors.add(question_id,"Spørgsmål #{q.position} mangler at blive udfyldt")
  55. end
  56.  
  57. # validates_numericality_of :answer, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 5
  58. end
  59.  
  60. ## Rka_completion model:
  61.  
  62. class RkaCompletion < ActiveRecord::Base
  63. has_many :rka_answers
  64. belongs_to :student
  65. belongs_to :rka_questionaire
  66. after_validation :merge_children_errors
  67.  
  68. accepts_nested_attributes_for :rka_answers, :allow_destroy => true
  69. def to_simile
  70. "{ \"start\": new Date(#{date.strftime '%Y,%m,%d'}),\"title\":\"Udfyldt type af RKA\",\"classname\":\"rkacompletion\" }"
  71. end
  72.  
  73. def merge_children_errors
  74. errors.clear
  75. rka_answers.each do |answer|
  76. answer.errors.map {|x| errors.add *x}
  77. end
  78. end
  79.  
  80. def error?(question_id)
  81. errors.each do |key, value|
  82. return true if key.to_i == question_id.to_i
  83. end
  84. false
  85. end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment