How would I be able to make a signup page with ruby on rails?
Like, I have a beta page and a user enters their email address and then I can add it to the database.
Also, I could send them an email confirming their signup
EDIT: I want something real simple. Like, just plain adding a row in a database simple. I don't need a password box and a username box because that just further complicates things. I'm a beginner so I like to have things simple.
At the terminal:
$ rails new foobar
$ rm public/index.html
$ git init
$ git commit -m "Initial commit"
$ rails g scaffold subscription email:string
Open up your editor:
# app/models/subscription.rb
class Subscription < ActiveRecord::Base
validates :email, :presence => true # optionally validate format of email
end
# app/controllers/subscriptions_controller.rb
class SubscriptionsController < ApplicationController
def new
@subscription = Subscription.new
end
def create
@subscription = Subscription.new params[:subscription]
if @subscription.save
# queue a task to send a confirmation email (see below)
Resque.enqueue(SendConfirmationEmail, @subscription.email)
redirect_to root_path, :notice => 'Thanks for signing up.'
else
render :new
end
end
end
You can delete all of the other methods/actions from your SubscriptionsController, and you can cleanup routes.rb
by restricting the actions available on the subscriptions
resource with resources :subscriptions, :only => [:new, :create]
.
This doesn't really cover how to send the email. There are a lot of ways to do it, and the best practice is to not send it in the request/response flow for performance/responsiveness. I've got a line in there queuing a Resque job to do it, but you could easily add DelayedJob or another deferred/async process tool there instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With