开发者

Ruby on rails super simple signup page

How would I be able to make a signup page wit开发者_StackOverflow中文版h 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.


This is the kind of thing that is very easy to do in Rails and you shouldn't need any extra gems. Here are the steps you will need to do:

  • Use a migration to create a model (i.e. database table) called Signup with a string field called "email".
  • Create an action that can be called with a "GET" request. This action will return the sign up form.
  • Create the view that serves as the signup form. It should have an HTML form element (<form method="POST">...</form>) that contains a text box (<input type="text" .../>) in it and a submit button (<input type="submit" />). Rails has all sorts of helper methods that will help you make those HTML tags, but you don't have to use them if you don't want to.
  • Create an action that can be called with a "POST" request that processes the form and adds the info to the database.

The action can be very simple:

def create_signup
  Signups.create! :email => params[:email]
end

Does this make sense? Now that I have given you the general guide, you should be able to ask new questions that are more focussed on specific parts that you don't know how to do. You should also search the web because there are probably tutorials available for all of these steps.


This question is too broad to answer with the code itself, but here are some great links to point you in the right direction:

Devise (most common Rails auth & signup plugin):
https://github.com/plataformatec/devise
Devise tutorial:
http://railscasts.com/episodes/209-introducing-devise
Mailer tutorial:
http://railscasts.com/episodes/206-action-mailer-in-rails-3
Other Auth tutorials:
http://railscasts.com/episodes/250-authentication-from-scratch
http://railscasts.com/episodes/270-authentication-in-rails-3-1


I made an app for this. Launchrock offers a good solution, but if you have two types of users then you are hosed. Our future site will have multiple types of users and we wanted to record which type they were. So we made an app and it's on Github for the world to use and change. :D Fork and clone the repo to make it your own. I included social plugin's as well. It's not styled and you'll have to change a few things to fit your needs, but I tried to make note of those in the README.rd.

Launchpage-rails


You could have a look at the 'Devise' gem -

https://github.com/plataformatec/devise

Railscasts episode on 'Devise'

http://railscasts.com/episodes/209-introducing-devise

The excellent 'Rails Tutorial' also takes you through building a sign-up/authentication system from scratch -

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜