Skip to content

Commit

Permalink
Updated gem file for rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Resella committed May 13, 2011
1 parent 0dd54f4 commit 6b3f438
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
11 changes: 11 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
class UsersController < ApplicationController
def new
@user = User.new
@title = "Sign up"
end

def show
@user = User.find(params[:id])
@title = @user.name
end

def create
@user = User.new(params[:user])
if @user.save
#Handle a successful save
else
@title = "Sign up"
@render = 'new'
end
end

end
12 changes: 12 additions & 0 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
26 changes: 24 additions & 2 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
<h1>Users#new</h1>
<p>Find me in app/views/users/new.html.erb</p>
<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
48 changes: 47 additions & 1 deletion public/stylesheets/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,50 @@ td.sidebar {
.profile img.gravatar {
border: 1px solid #999;
margin-bottom: -15px;
}
}

div.field, div.actions {
margin-bottom: 10px;
}

/*error code */
.field_with_errors {
margin-top: 10px;
padding: 2px;
background-color: red;
display: table;
}

.field_with_errors label {
color: #fff;
}

#error_explanation {
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
}

#error_explanation p {
color: #333;
margin-bottom: 0;
padding: 5px;
}

#error_explanation ul li {
font-size: 12px;
list-style: square;
}
28 changes: 28 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,32 @@
response.should have_selector("title", :content => "Sign up")
end
end

describe "POST 'create'" do

describe "failure" do

before(:each) do
@attr = { :name => "", :email => "", :password => "",
:password_confirmation => "" }
end

it "should not create a user" do
lambda do
post :create, :user => @attr
end.should_not change(User, :count)
end

it "should have the right title" do
post :create, :user => @attr
response.should have_selector("title", :content => "Sign up")
end

it "should render the 'new' page" do
post :create, :user => @attr
response.should render_template('new')
end
end
end

end

0 comments on commit 6b3f438

Please sign in to comment.