Guest User

Untitled

a guest
May 22nd, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. Request
  2.  
  3. Parameters:
  4.  
  5. {"utf8"=>"✓",
  6. "_method"=>"put",
  7. "authenticity_token"=>"auxoUaTmvkvRvLUUh3kUP0lzYo9aTotajjYVfy01OE0=",
  8. "user"=>{"first_name"=>"Fabian",
  9. "last_name"=>"Jara",
  10. "email"=>"admin@example.com",
  11. "age"=>"35",
  12. "position_primary"=>"54ed0c136c6f6337ff0a0000",
  13. "position_secundary"=>"54ed0c136c6f6337ff0a0011"},
  14. "commit"=>"Update"}
  15.  
  16. class User
  17. include Mongoid::Document
  18. include Mongoid::Timestamps
  19. include User::AuthDefinitions
  20. include User::Roles
  21. include Mongoid::Paperclip
  22.  
  23. has_many :identities
  24.  
  25. embeds_one :position_primary, class_name: "Position"
  26. embeds_one :position_secundary, class_name: "Position"
  27.  
  28. accepts_nested_attributes_for :position_primary, :position_secundary
  29.  
  30. has_mongoid_attached_file :image
  31.  
  32. validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  33.  
  34. field :first_name, type: String
  35. field :last_name, type: String
  36. field :roles_mask, type: Integer
  37. field :age, type: Integer
  38.  
  39. end
  40.  
  41. class Position
  42. include Mongoid::Document
  43.  
  44. field :name, type: String
  45. field :status, type: Boolean
  46.  
  47. embedded_in :users
  48. end
  49.  
  50. <h2>Edit <%= resource_name.to_s.humanize %></h2>
  51. <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :class => 'form-vertical' }) do |f| %>
  52. <%= f.input :first_name, :autofocus => true %>
  53. <%= f.input :last_name %>
  54. <%= f.input :email, :required => true %>
  55. <%= f.input :age %>
  56. <%= f.file_field :image %> <br>
  57.  
  58. <%= f.select :position_primary, Position.all.collect {|p| [ p.name, p.id ] }, :prompt => "Select a position" %>
  59. <br>
  60.  
  61. <%= f.select :position_secundary, Position.all.collect {|p| [ p.name, p.id ] }, :prompt => "Select a position" %>
  62. <br>
  63. <% if current_user.password_required? %>
  64. <%= f.input :password, :autocomplete => "off", :hint => "leave it blank if you don't want to change it", :required => false %>
  65. <%= f.input :password_confirmation, :required => false %>
  66. <%= f.input :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
  67. <% end %>
  68. <%= f.button :submit, 'Update', :class => 'btn-primary' %>
  69. <% end %>
  70. <h3>Cancel my account</h3>
  71. <p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %>.</p>
  72.  
  73. class Users::RegistrationsController < Devise::RegistrationsController
  74. before_filter :resource_params, if: :devise_controller?
  75.  
  76. def resource_params
  77. devise_parameter_sanitizer.for(:sign_up) {|user| user.permit(:first_name, :last_name, :email, :password, :password_confirmation)}
  78. devise_parameter_sanitizer.for(:account_update) {|user| user.permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)}
  79. end
  80.  
  81. def update
  82. authorize! :update, @user, :message => 'Not authorized as an administrator.'
  83. respond_to do |format|
  84.  
  85. if @user.update_attributes(user_params)
  86. format.html { redirect_to @user, notice: 'User was successfully updated.' }
  87. else
  88. format.html { render action: 'edit' }
  89. end
  90. end
  91. end
  92.  
  93. # Never trust parameters from the scary internet, only allow the white list through.
  94. def user_params
  95. params.require(:user).permit(:email, :first_name, :last_name, :image, :age, :position_primary, :position_secundary, :roles => [])
  96. end
  97.  
  98. private :resource_params
  99. end
  100.  
  101. u2 = User.new(name: 'my_name', email: 'my_email@domain.com', password: 'pass', password_confirmation: 'pass', rol: [Rol.new(nombre: 'admin', description: '...')])
  102. u2.save!
  103.  
  104. u2 = User.new(name: 'my_name', email: 'my_email@domain.com', password: 'pass', password_confirmation: 'pass', rol: Rol.new(nombre: 'admin', description: '...'))
  105. u2.save!
  106.  
  107. class MyModel
  108. include Mongoid::Document
  109.  
  110. field :my_field, type: String
  111. end
  112.  
  113. class MyModel
  114. include Mongoid::Document
  115.  
  116. embeds_one :my_field, class_name: 'MyNestedModel'
  117. end
Add Comment
Please, Sign In to add comment