Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. ##users_controller.rb
  2. def new
  3. @new_users = []
  4. params[:id].to_i.times do
  5. @new_users << User.new
  6. end
  7. end
  8.  
  9. ##new.rhtml
  10. 1 <div id="usersForm">
  11. 2
  12. 3 <%= error_messages_for :user %>
  13. 4
  14. 5 <%= start_form_tag :action => 'new' %>
  15. 6 <table>
  16. 7 <thead>
  17. 8 <tr>
  18. 9 <th>Nimi</th>
  19. 10 <th>Salasõna</th>
  20. 11 <th>Salasõna uuesti</th>
  21. 12 <th>Amet</th>
  22. 13 </tr>
  23. 14 </thead>
  24. 15 <tbody>
  25. 16 <% @new_users.each_with_index do |user, index| %>
  26. 17 <%= render :partial => 'fields', :locals => { :user => user, :index => index } %>
  27. 18 <% end %>
  28. 19 </tbody>
  29. 20 <tfoot>
  30. 21 <tr>
  31. 22 <td colspan="4">
  32. 23 <%= submit_tag "Lisa kasutaja(d)" %>
  33. 24 </td>
  34. 25 </tr>
  35. 26 </tfoot>
  36. 27 </table>
  37. 28 <%= end_form_tag %>
  38. 29
  39. 30 </div>
  40.  
  41. ##_fields.rhtml
  42. 1 <% fields_for "user[#{index}]", user do |f| %>
  43. 2 <tr>
  44. 3 <td><%= f.text_field :login, :size => 14 %></td>
  45. 4 <td><%= f.password_field :password, :size => 14 %></td>
  46. 5 <td><%= f.password_field :password_confirmation, :size => 14 %></td>
  47. 6 <td><%= form_permissions_select(f) %></td>
  48. 7 </tr>
  49. 8 <% end %>
  50.  
  51. ## Error:
  52. stack level too deep
  53.  
  54. Extracted source (around line #0):
  55.  
  56. 1: <% fields_for "user[#{index}]", user do |f| %>
  57. 2: <tr>
  58. 3: <td><%= f.text_field :login, :size => 14 %></td>
  59.  
  60. Trace:
  61. /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1777:in `method_missing'
  62. /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1778:in `method_missing'
  63. /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1581:in `hash'
  64. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:455:in `create_template_source'
  65. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:503:in `compile_template'
  66. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:307:in `compile_and_render_template'
  67. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in `render_template'
  68. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:in `render_file'
  69. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:266:in `render'
  70. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/partials.rb:59:in `render_partial'
  71. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:29:in `benchmark'
  72. /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
  73. /usr/lib/ruby/1.8/benchmark.rb:307:in `realtime'
  74. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:29:in `benchmark'
  75. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/partials.rb:58:in `render_partial'
  76. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:278:in `render'
  77. #{RAILS_ROOT}/app/views/users/new.rhtml:17:in `_run_rhtml_users_new'
  78. #{RAILS_ROOT}/app/views/users/new.rhtml:16:in `_run_rhtml_users_new'
  79. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in `compile_and_render_template'
  80. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in `render_template'
  81. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:in `render_file'
  82. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:726:in `render_file'
  83. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:648:in `render_with_no_layout'
  84. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/layout.rb:245:in `render_without_benchmark'
  85. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:in `render'
  86. /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
  87. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:in `render'
  88. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:942:in `perform_action_without_filters'
  89. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
  90. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
  91. /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
  92. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
  93. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in `perform_action'
  94. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `process_without_filters'
  95. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
  96. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in `process'
  97. /usr/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in `dispatch'
  98. /usr/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:in `handle_dispatch'
  99. /usr/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:in `service'
  100. /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  101. /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  102. /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  103. /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  104. /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
  105. /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
  106. /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
  107. /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
  108. /usr/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:in `dispatch'
  109. /usr/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59
  110. /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
  111. /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
  112. /usr/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30
  113. /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
  114. /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
  115. script/server:3
  116.  
  117. ##user.rb
  118. require 'digest/sha1'
  119. class User < ActiveRecord::Base
  120.  
  121. has_many :messages
  122.  
  123. belongs_to :permission
  124.  
  125. # Virtual attribute for the unencrypted password
  126. attr_accessor :password
  127.  
  128. validates_presence_of :login
  129. validates_presence_of :password, :if => :password_required?
  130. validates_presence_of :password_confirmation, :if => :password_required?
  131. validates_length_of :password, :within => 4..40, :if => :password_required?
  132. validates_confirmation_of :password, :if => :password_required?
  133. validates_length_of :login, :within => 3..40
  134. validates_uniqueness_of :login, :case_sensitive => false
  135. before_save :encrypt_password
  136.  
  137. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  138. def self.authenticate(login, password)
  139. u = find_by_login(login) # need to get the salt
  140. u && u.authenticated?(password) ? u : nil
  141. end
  142.  
  143. # Encrypts some data with the salt.
  144. def self.encrypt(password, salt)
  145. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  146. end
  147.  
  148. # Encrypts the password with the user salt
  149. def encrypt(password)
  150. self.class.encrypt(password, salt)
  151. end
  152.  
  153. def authenticated?(password)
  154. crypted_password == encrypt(password)
  155. end
  156.  
  157. def remember_token?
  158. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  159. end
  160.  
  161. # These create and unset the fields required for remembering users between browser closes
  162. def remember_me
  163. self.remember_token_expires_at = 2.weeks.from_now.utc
  164. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  165. save(false)
  166. end
  167.  
  168. def forget_me
  169. self.remember_token_expires_at = nil
  170. self.remember_token = nil
  171. save(false)
  172. end
  173.  
  174. def is_admin?; false; end;
  175. def is_teacher?; false; end;
  176. def is_student?; false; end;
  177. def is_parent?; false; end;
  178. def schoolclass; 0; end;
  179.  
  180. protected
  181. # before filter
  182. def encrypt_password
  183. return if password.blank?
  184. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  185. self.crypted_password = encrypt(password)
  186. end
  187.  
  188. def password_required?
  189. crypted_password.blank? || !password.blank?
  190. end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement