Guest User

Untitled

a guest
May 3rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. ## UserController
  2. def new
  3. @user = User.new
  4. @artist = Artist.new
  5. end
  6.  
  7. def create
  8. logout_keeping_session!
  9. @user = User.new(params[:user])
  10. @artist = @user.build_artist(params[:artist])
  11. @user.register! if @user && @user.valid?
  12. success = @user && @user.valid?
  13. if success && @user.errors.empty?
  14. redirect_back_or_default('/')
  15. flash[:notice] = "Thanks for signing up! We're sending you an email with your activation code."
  16. else
  17. flash[:error] = "We couldn't set up that account, sorry. Please try again, or contact an admin (link is above)."
  18. render :action => 'new'
  19. end
  20. end
  21.  
  22. ## User model
  23.  
  24. class User < ActiveRecord::Base
  25. has_one :artist
  26.  
  27. validate do |user|
  28. user.validate_artist
  29. end
  30.  
  31. def validate_artist
  32. unless artist.valid?
  33. artist.errors.each_full { |msg| errors.add(:artist, "is invalid: #{msg}") }
  34. end
  35. end
  36.  
  37. # ...
  38. end
  39.  
  40. ## Artist Model
  41.  
  42. class Artist < ActiveRecord::Base
  43. validates_presence_of :name
  44. validates_uniqueness_of :name, :case_sensitive => false
  45. end
  46.  
  47.  
  48.  
  49. ## User:View:New
  50.  
  51. <h1>Sign up as a new user</h1>
  52. <% @user.password = @user.password_confirmation = nil %>
  53.  
  54. <%= error_messages_for :user %>
  55. <% form_for :user, :url => users_path do |f| -%>
  56. <p><label for="email">Email (this will be your login name)</label><br/>
  57. <%= f.text_field :email %></p>
  58.  
  59. <p><label for="name">Artist Name</label><br/>
  60. <% fields_for "artist", @artist do |a| %>
  61. <%= a.text_field :name %>
  62. <% end %>
  63. <p><label for="password">Password</label><br/>
  64. <%= f.password_field :password %></p>
  65.  
  66. <p><label for="password_confirmation">Confirm Password</label><br/>
  67. <%= f.password_field :password_confirmation %></p>
  68.  
  69. <p><%= submit_tag 'Sign up' %></p>
  70. <% end -%>
Add Comment
Please, Sign In to add comment