Some notes from http://www.railstutorial.org/book/_single-page#cha-sign_up
Image may be NSFW.
Clik here to view.
config/routes.rb
match '/signup', to: 'users#new', via: 'get' end
Gemfile
group :test do gem 'selenium-webdriver', '2.35.1' gem 'capybara', '2.1.0' gem 'factory_girl_rails', '4.2.0' end
> bundle install
spec/factories.rb
FactoryGirl.define do factory :user do name "Michael Hartl" email "michael@example.com" password "foobar" password_confirmation "foobar" end end
config/environments/test.rb
SampleApp::Application.configure do # Speed up tests by lowering bcrypt's cost function. ActiveModel::SecurePassword.min_cost = true end
app/views/users/show.html.erb
<% provide(:title, @user.name) %> <h1> <%= gravatar_for @user %> <%= @user.name %> </h1>
app/helpers/users_helper.rb
module UsersHelper # Returns the Gravatar (http://gravatar.com/) for the given user. def gravatar_for(user) gravatar_id = Digest::MD5::hexdigest(user.email.downcase) gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}" image_tag(gravatar_url, alt: user.name, class: "gravatar") end end
app/views/users/new.html.erb
<%= provide(:title, 'Sign Up') %> <div class="container"> <%= form_for(@user, :html => {:class => 'form-signin'}) do |f| %> <h2 class="form-signin-heading">Sign Up</h2> <%= render 'shared/error_messages' %> <%= f.label :name %> <%= f.text_field :name %> <%= f.label :email %> <%= f.text_field :email %> <%= f.label :password %> <%= f.password_field :password %> <%= f.label :password_confirmation, "Confirmation" %> <%= f.password_field :password_confirmation %> <div class="text-centered"> <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> </div> <% end %> </div>
app/views/shared/_error_messages.html.erb
<% if @user.errors.any? %> <div id="error_explanation"> <div class="alert alert-error"> The form contains <%= pluralize(@user.errors.count, "error") %>. </div> <ul> <% @user.errors.full_messages.each do |msg| %> <li>* <%= msg %></li> <% end %> </ul> </div> <% end %>
app/assets/stylesheets/custom.css.scss
/* forms */ #error_explanation { color: #f00; ul { list-style: none; margin: 0 0 18px 0; } } .field_with_errors { @extend .control-group; @extend .error; }
app/assets/stylesheets/signin.css.scss
/* sign in */ .form-signin { max-width: 300px; padding: 19px 29px 29px; margin: 0 auto 20px; background-color: #fff; border: 1px solid #e5e5e5; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05); -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05); box-shadow: 0 1px 2px rgba(0,0,0,.05); } .form-signin .form-signin-heading, .form-signin .checkbox { margin-bottom: 10px; } .form-signin input[type="text"], .form-signin input[type="password"] { font-size: 16px; height: auto; margin-bottom: 15px; padding: 7px 9px; }
Adding flash
<strong>app/views/layouts/application.html.erb</strong> <!DOCTYPE html> <html> . . . <body> <%= render 'layouts/header' %> <div class="container"> <% flash.each do |key, value| %> <div class="alert alert-<%= key %>"><%= value %></div> <% end %> <%= yield %> <%= render 'layouts/footer' %> <%= debug(params) if Rails.env.development? %> </div> . . . </body> </html>
app/controllers/users_controller.rb
class UsersController < ApplicationController def create @user = User.new(user_params) if @user.save flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end private def user_params params.require(:user).permit(:name, :email, :password_digest, :admin, :password, :password_confirmation) end end
spec/requests/user_pages_spec.rb
require 'spec_helper' describe 'User pages' do subject { page } describe 'signup page' do before { visit signup_path } it { should have_content('Sign Up') } it { should have_title('AC Sign Up') } end describe "signup" do before { visit signup_path } let(:submit) { "Create my account" } describe "with invalid information" do it "should not create a user" do expect { click_button submit }.not_to change(User, :count) end end describe "with valid information" do before do fill_in "Name", with: "Example User" fill_in "Email", with: "user@example.com" fill_in "Password", with: "foobar" fill_in "Confirmation", with: "foobar" end it "should create a user" do expect { click_button submit }.to change(User, :count).by(1) end describe "after saving the user" do before { click_button submit } let(:user) { User.find_by(email: 'user@example.com') } # it { should have_link('Sign out') } it { should have_title(user.name) } it { should have_selector('div.alert.alert-success', text: 'Welcome') } end end end end
Filed under: rails Tagged: rails Image may be NSFW.
Clik here to view.
Clik here to view.
