Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. class Person < ActiveRecord::Base
  2. validates :terms_of_service, acceptance: { accept: 'yes' }
  3. end
  4.  
  5. class UsersController < ApplicationController
  6. before_filter :authenticate_user!
  7. before_filter :admin_only, :except => :show
  8.  
  9. def index
  10. @users = User.all
  11. end
  12.  
  13. def show
  14. @user = User.find(params[:id])
  15. unless current_user.admin?
  16. unless @user == current_user
  17. redirect_to :back, :alert => "Access denied."
  18. end
  19. end
  20. end
  21.  
  22. def update
  23. @user = User.find(params[:id])
  24. if @user.update_attributes(secure_params)
  25. redirect_to users_path, :notice => "User updated."
  26. else
  27. redirect_to users_path, :alert => "Unable to update user."
  28. end
  29. end
  30.  
  31. def destroy
  32. user = User.find(params[:id])
  33. user.destroy
  34. redirect_to users_path, :notice => "User deleted."
  35. end
  36.  
  37. private
  38.  
  39. def user_params
  40. params.require(:user).permit(:name, :email, :terms_accepted)
  41. end
  42.  
  43. def admin_only
  44. unless current_user.admin?
  45. redirect_to :back, :alert => "Access denied."
  46. end
  47. end
  48.  
  49. def secure_params
  50. params.require(:user).permit(:role)
  51. end
  52. end
  53.  
  54. ---
  55. class User < ActiveRecord::Base
  56. validates :terms_accepted, acceptance: { accept: 'yes' }
  57.  
  58. enum role: [:user, :vip, :admin]
  59. after_initialize :set_default_role, :if => :new_record?
  60.  
  61.  
  62. def set_default_role
  63. self.role ||= :user
  64. end
  65.  
  66. # Include default devise modules. Others available are:
  67. # :confirmable, :lockable, :timeoutable and :omniauthable
  68. devise :invitable, :database_authenticatable, :registerable, :confirmable,
  69. :recoverable, :rememberable, :trackable, :validatable
  70. end
  71. ---
  72.  
  73. <div class="row">
  74. <div class="col-md-6">
  75. <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
  76. <h2 class="form-signin-heading">Sign Up</h2>
  77. <%= devise_error_messages! %>
  78. <div class="form-group">
  79. <%= f.label :name %>
  80. <%= f.text_field :name, :autofocus => true, class: 'form-control' %>
  81. </div>
  82. <div class="form-group">
  83. <%= f.label :email %>
  84. <%= f.email_field :email, class: 'form-control' %>
  85. </div>
  86. <div class="form-group">
  87. <%= f.label :password %>
  88. <%= f.password_field :password, class: 'form-control' %>
  89. </div>
  90. <div class="form-group">
  91. <%= f.label :confirm_password %>
  92. <%= f.password_field :password_confirmation, class: 'form-control' %>
  93. </div>
  94. <div class="form-group">
  95. <%= f.check_box :terms_accepted%>
  96. <%= f.label :accept_terms %>
  97. <%= link_to 'Terms Of Use', '/TermsOfUse.html', :target => "_blank" %>
  98. </div>
  99. <div class="form-group">
  100. <%= f.submit 'Sign Up', :class => 'btn btn-lg btn-login' %>
  101. </div>
  102. <div class="form-group">
  103. <%= render "devise/shared/links" %>
  104. </div>
  105. <% end %>
  106. </div>
  107.  
  108. Started GET "/" for ::1 at 2015-06-29 13:55:55 -0700
  109. Processing by VisitorsController#index as HTML
  110. (0.5ms) SELECT COUNT(*) FROM "users"
  111. Rendered visitors/index.html.erb within layouts/application (1.6ms)
  112. Rendered layouts/_flatlabnavbartop.html.haml (16.2ms)
  113. Completed 200 OK in 347ms (Views: 345.7ms | ActiveRecord: 0.5ms)
  114.  
  115. Started GET "/users/sign_up" for ::1 at 2015-06-29 13:56:00 -0700
  116. Processing by DeviseInvitable::RegistrationsController#new as HTML
  117. Rendered /usr/local/rvm/gems/ruby-2.2.1@suits6/gems/devise-3.4.1/app/views/devise/shared/_links.html.erb (1.4ms)
  118. Rendered devise/registrations/new.html.erb within layouts/application (51.9ms)
  119. Rendered layouts/_flatlabnavbartop.html.haml (1.9ms)
  120. Completed 200 OK in 279ms (Views: 277.9ms | ActiveRecord: 0.0ms)
  121.  
  122.  
  123. Started POST "/users" for ::1 at 2015-06-29 13:56:29 -0700
  124. Processing by DeviseInvitable::RegistrationsController#create as HTML
  125. Parameters: {"utf8"=>"✓", "authenticity_token"=>"4t0UPaQhI/0HcsqC3RkBrcWQWjhWzKojZLvrMloObPSAiapVc46bvxT5TGePh4v2IUCi8QbdVuMWuQsyzyFmdg==", "user"=>{"name"=>"Maude Username", "email"=>"maude@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms_accepted"=>"1"}, "commit"=>"Sign Up"}
  126.  
  127. Unpermitted parameter: terms_accepted
  128.  
  129. User Load (119.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 AND "users"."encrypted_password" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["email", "maude@gmail.com"], ["encrypted_password", ""]]
  130.  
  131. (0.2ms) BEGIN
  132. User Exists (16.6ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'maude@gmail.com' LIMIT 1
  133.  
  134. (8.3ms) ROLLBACK
  135. Rendered /usr/local/rvm/gems/ruby-2.2.1@suits6/gems/devise-3.4.1/app/views/devise/shared/_links.html.erb (0.7ms)
  136.  
  137. Rendered devise/registrations/new.html.erb within layouts/application (18.5ms)
  138.  
  139. Rendered layouts/_flatlabnavbartop.html.haml (2.9ms)
  140.  
  141. Completed 200 OK in 733ms (Views: 383.5ms | ActiveRecord: 144.9ms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement