Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### app/models/user.erb
- ```ruby
- class User < ActiveRecord::Base
- # All of the fields listed here are required
- validates_presence_of :email, :avatar_url, :username, :password
- # All of these fields must be unique (i.e. can't register the same email twice)
- validates_uniqueness_of :email, :username
- end
- ```
- ### app/views/register.erb
- ```html
- <form method="post" action="/register">
- <label for="email">Email:</label>
- <input type="email" name="email" id="email" required>
- <br><br>
- <label for="username">Username:</label>
- <input type="text" name="username" id="username" required>
- <br><br>
- <label for="avatar_url">Avatar URL:</label>
- <input type="url" name="avatar_url" id="avatar_url" required>
- <br><br>
- <label for="password">Password:</label>
- <input type="password" name="password" id="password" required>
- <br><br>
- <input type="submit">
- </form>
- ```
- ### app/actions.rb
- ```ruby
- get '/register' do
- erb :register
- end
- post '/register' do
- # Get the values the user submitted in the form
- email = params[:email]
- username = params[:username]
- avatar_url = params[:avatar_url]
- password = params[:password]
- # Create a new instance of the user class
- @user = User.new(email: email, username: username, avatar_url: avatar_url, password: password)
- # Try saving the user to the database
- # Remember that .save automatically runs validations and returns true or false
- if @user.save
- # Saving worked, so render a success page!
- erb :registration_success
- else
- # Saving did not work, show a failure page
- erb :registration_failure
- end
- end
- ```
- ### app/views/registration_success.erb
- ```html
- Yay! It worked!
- ```
- ### app/views/registration_failure.erb
- ```html
- No dice, try again.
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement