Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. class AdminController < ApplicationController
  2. before_action :authenticate_user!
  3. def index
  4.  
  5. request_hash = {
  6. :name => params[:name],
  7. :email => params[:email],
  8. :role => params[:role],
  9. :password => params[:password],
  10. :password_confirmation => params[:password_confirmation]
  11. }
  12.  
  13. @user = User.create!(request_hash)
  14. @user.save
  15. end
  16. end
  17.  
  18. class ApplicationController < ActionController::Base
  19. # Prevent CSRF attacks by raising an exception.
  20. # For APIs, you may want to use :null_session instead.
  21. protect_from_forgery with: :exception
  22. before_action :configure_permitted_parameters, if: :devise_controller?
  23.  
  24.  
  25.  
  26. def after_sign_in_path_for(resource)
  27. session[:previous_url] ||
  28. if current_user.role == "admin"
  29. admin_index_path
  30. else
  31. jolex_index_path
  32. end
  33.  
  34. end
  35.  
  36. protected
  37.  
  38. def configure_permitted_parameters
  39. devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :role])
  40. devise_parameter_sanitizer.permit(:account_update, keys: [:name, :country])
  41. end
  42.  
  43.  
  44. end
  45.  
  46. class User < ActiveRecord::Base
  47. # Include default devise modules. Others available are:
  48. # :confirmable, :lockable, :timeoutable and :omniauthable
  49. devise :database_authenticatable, :registerable,
  50. :recoverable, :rememberable, :trackable, :validatable
  51.  
  52. devise :database_authenticatable, :registerable,
  53. :recoverable, :rememberable, :trackable, :validatable
  54. end
  55.  
  56. <%= form_for resource, as: resource_name, url: registration_path(resource_name) do |f| %>
  57.  
  58. <div class="field">
  59. <%= f.label :name %><br />
  60. <%= f.text_field :name %>
  61. </div>
  62.  
  63. <div class="field">
  64. <%= f.label :email %><br />
  65. <%= f.email_field :email, autofocus: true %>
  66. </div>
  67.  
  68. <div class="field">
  69. <%= f.label :password %>
  70. <% if @minimum_password_length %>
  71. <em>(<%= @minimum_password_length %> characters minimum)</em>
  72. <% end %><br />
  73. <%= f.password_field :password, autocomplete: "off" %>
  74. </div>
  75.  
  76. <div class="field">
  77. <%= f.label :password_confirmation %><br />
  78. <%= f.password_field :password_confirmation, autocomplete: "off" %>
  79. </div>
  80.  
  81. <div class="field">
  82.  
  83. <%= f.select(:role) do %>
  84. <% [['Admin', "admin"], ['User', "user"]].each do |c| -%>
  85. <%= content_tag(:option, c.first, value: c.last) %>
  86. <% end %>
  87. <% end %>
  88. </div>
  89.  
  90. <div class="actions">
  91. <%= f.submit "Sign up" %>
  92. </div>
  93. <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement