Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File: features/support/path.rb
- when /login/
- login_path
- when /signup/
- signup_path
- # File: features/support/user_helpers.rb
- module UserHelpers
- def create_user( options = {} )
- args = {
- :username => 'subscriber',
- :password => 'secret',
- :password_confirmation => 'secret',
- }.merge( options )
- args[:email] ||= "#{args[:username]}@example.com"
- user = User.create!(args)
- # :create syntax for restful_authentication w/ aasm. Tweak as needed.
- # user.activate!
- user
- end
- def log_in_as( username )
- visit "/login"
- fill_in("user_session_username", :with => username)
- fill_in("password", :with => 'secret')
- click_button("Log in")
- end
- end
- World(UserHelpers)
- # File: features/user.feature
- Feature: User authentication
- In order to access the site
- As a user
- I must register and log in
- Scenario: Register as new user
- Given I am on the homepage
- When I follow "Sign up!"
- Then I should be on the signup page
- When I fill in the following:
- | username | subscriber |
- | email | subscriber@example.com |
- | password | secret |
- | confirm password | secret |
- And I press "Sign up"
- Then I should be on the homepage
- And I should see "Thank you for signing up! You are now logged in."
- Scenario: Edit profile
- Given a user is logged in as "subscriber"
- And I am on the homepage
- When I follow "My profile"
- Then I should see "Show User Profile"
- When I follow "Edit Profile"
- Then I should see "Edit User Profile"
- When I fill in "email" with "changed@example.com"
- And I press "Submit"
- Then I should be on the homepage
- And I should see "Successfully updated user profile."
- # File: app/layouts/_user_nav.html.erb
- <div id="user_nav">
- <%= link_to "Home", root_path %> |
- <% if current_user %>
- <%= link_to "My profile", user_path(:current) %> |
- <%= link_to "Logout", logout_path %>
- <% else %>
- <%= link_to "Sign up", signup_path %> |
- <%= link_to "Login", login_path %>
- <% end %>
- </div>
- # File: app/controllers/users_controller.rb
- class UsersController < ApplicationController
- before_filter :login_required, :except => [:new, :create]
- before_filter :find_user, :except => [:new, :create]
- def new
- @user = User.new
- end
- def create
- @user = User.new(params[:user])
- if @user.save
- flash[:notice] = "Thank you for signing up! You are now logged in."
- redirect_to root_url
- else
- render :action => 'new'
- end
- end
- def show
- end
- def edit
- end
- def update
- if @user.update_attributes(params[:user])
- flash[:notice] = "Successfully updated user profile."
- redirect_to root_url
- else
- render :action => 'edit'
- end
- end
- private
- def find_user
- @user = current_user
- end
- end
- # File: app/views/users/show.html.erb
- <% title "Show User Profile" %>
- <p>
- <strong>Username:</strong>
- <%=h @user.username %>
- </p>
- <p>
- <strong>Email:</strong>
- <%=h @user.email %>
- </p>
- <p>
- <%= link_to "Edit Profile", edit_user_path(@user) %>
- </p>
- # File: app/views/users/edit.html.erb
- <% title "Edit User Profile" %>
- <% form_for @user do |f| %>
- <%= f.error_messages %>
- <p>
- <%= f.label :email %><br />
- <%= f.text_field :email %>
- </p>
- <p>
- <%= f.label :password %><br />
- <%= f.password_field :password, :value => nil, :autocomplete => 'off' %>
- </p>
- <p>
- <%= f.label :password_confirmation %><br />
- <%= f.password_field :password_confirmation %>
- </p>
- <p><%= f.submit "Submit" %></p>
- <p><%= link_to "Cancel", root_url %>
- <% end %>
Add Comment
Please, Sign In to add comment