You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classUser < ActiveRecord::Base# All of the fields listed here are requiredvalidates_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,:usernameend
app/views/register.erb
<% if @user.present? && @user.errors.present? %>
Error! The following errors prevented saving:
<ul><%@user.errors.full_messages.eachdo |error| %><li><%=error%></li><%end%></ul><%end%><formmethod="post"><labelfor="email">Email:</label><inputtype="email" name="email" id="email" required><br><br><labelfor="username">Username:</label><inputtype="text" name="username" id="username" required><br><br><labelfor="avatar_url">Avatar URL:</label><inputtype="url" name="avatar_url" id="avatar_url" required><br><br><labelfor="password">Password:</label><inputtype="password" name="password" id="password" required><br><br><inputtype="submit"></form>
app/actions.rb
get'/register'doerb:registerendpost'/register'do# Get the values the user submitted in the formemail=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 falseif@user.save# Saving worked, so render a success page!erb:registration_completeelse# Saving did not work, show registration form again with errorserb:registerendend
app/views/registration_complete.erb
<p>
Hey there <%=@user.username%>,
</p><p>
Thanks for registering, you're all set! Enjoy the fins.
</p>