Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add feature: user can edit account info

  • Loading branch information...
commit b20f55a2829575179e5d16c9a036665e4f89fac7 1 parent 4504d36
@rociiu rociiu authored
View
16 app/controllers/account_controller.rb
@@ -0,0 +1,16 @@
+class AccountController < ApplicationController
+
+ before_filter :logged_in?
+
+ def show
+ @user = current_user
+ end
+
+ def update
+ @user = current_user
+ @user.update_attributes(params[:user])
+ flash[:notice] = "Successfully update account info!"
+ redirect_to '/account'
+ end
+
+end
View
1  app/controllers/application_controller.rb
@@ -1,7 +1,6 @@
class ApplicationController < ActionController::Base
protect_from_forgery
-
protected
def current_user
View
20 app/views/account/show.html.erb
@@ -0,0 +1,20 @@
+<div id="account">
+ <%= form_tag('/account', :method => :put) do %>
+ <h2>Account Info</h2>
+ <div class="field">
+ <label>Name</label>
+ <%= text_field_tag 'user[name]', @user.name %>
+ </div>
+ <div class="field">
+ <label>Nickname</label>
+ <%= text_field_tag 'user[nickname]', @user.nickname %>
+ </div>
+ <div class="field">
+ <label>Email</label>
+ <%= text_field_tag 'user[nickname]', @user.email %>
+ </div>
+ <div class="field">
+ <%= submit_tag 'Update' %>
+ </div>
+ <% end %>
+</div>
View
4 app/views/layouts/application.html.erb
@@ -13,7 +13,9 @@
<div id="signedin_box">
<a href="/browse">Browse</a>
<% if current_user %>
- <a href="/mygems">My Gems</a><a href="/sign_out">SignOut</a>
+ <a href="/mygems">My Gems</a>
+ <a href="/account">Account</a>
+ <a href="/sign_out">SignOut</a>
<% end %>
</div>
</div>
View
2  config/routes.rb
@@ -21,6 +21,8 @@
get "hooks/gems"
post "hooks/gems"
+ resource :account, :except => [:create, :new, :edit], :controller => :account
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
26 public/stylesheets/sass/style.sass
@@ -215,7 +215,7 @@ a.signin
font-size: 1.1em
font-weight: bold
-div.gem_item
+div.gem_item, #account
margin: 20px 0
border: 1px solid #eee
background: #fff
@@ -226,15 +226,35 @@ div.gem_item
border: 1px solid #999
@include rounded-corner(5px)
-div.gem_item .field
+div.gem_item .field, #account .field
margin: 10px 0
-div.gem_item .field label
+div.gem_item .field label, #account .field label
font-weight: bold
display: block
width: 200px
margin-right: 20px
+#account
+ padding: 20px
+
+#account h2
+ font-weight: bold
+ font-size: 15px
+ border-bottom: 1px solid #ccc
+ padding: 3px 0
+
+#account .field input[type='text']
+ padding: 2px 3px
+ width: 200px
+ border: 1px solid #ccc
+ padding: 5px
+ &:focus
+ border-color: #F0F8FB
+ -moz-box-shadow: 0 0 2px #F0F8FB
+ -webkit-box-shadow: 0 0 2px #F0F8FB
+ box-shadow: 0 0 2px #F0F8FB, 0 0 8px rgba(0,0,0,0.25) inset
+
.most_updated_gems, .most_subscribed_gems
width: 50%
margin-top: 30px
View
26 public/stylesheets/style.css
@@ -211,7 +211,7 @@ a.signin {
font-size: 1.1em;
font-weight: bold; }
-div.gem_item {
+div.gem_item, #account {
margin: 20px 0;
border: 1px solid #eeeeee;
background: white;
@@ -224,15 +224,35 @@ div.gem_item {
-moz-border-radius: 5px;
-webkit-border-radius: 5px; }
-div.gem_item .field {
+div.gem_item .field, #account .field {
margin: 10px 0; }
-div.gem_item .field label {
+div.gem_item .field label, #account .field label {
font-weight: bold;
display: block;
width: 200px;
margin-right: 20px; }
+#account {
+ padding: 20px; }
+
+#account h2 {
+ font-weight: bold;
+ font-size: 15px;
+ border-bottom: 1px solid #cccccc;
+ padding: 3px 0; }
+
+#account .field input[type='text'] {
+ padding: 2px 3px;
+ width: 200px;
+ border: 1px solid #cccccc;
+ padding: 5px; }
+ #account .field input[type='text']:focus {
+ border-color: #f0f8fb;
+ -moz-box-shadow: 0 0 2px #f0f8fb;
+ -webkit-box-shadow: 0 0 2px #f0f8fb;
+ box-shadow: 0 0 2px #f0f8fb, 0 0 8px rgba(0, 0, 0, 0.25) inset; }
+
.most_updated_gems, .most_subscribed_gems {
width: 50%;
margin-top: 30px; }
View
49 spec/controllers/account_controller_spec.rb
@@ -0,0 +1,49 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe AccountController do
+
+ describe "With user login" do
+
+ before(:each) do
+ @user = Factory(:user)
+ session[:user_id] = @user
+ end
+
+ describe "GET show" do
+
+ it "should show the user's profile" do
+ get :show
+
+ assigns[:user].should == @user
+ response.should be_success
+ end
+
+ end
+
+ describe "PUT update" do
+
+ it "should update user info" do
+ put :update, { 'user' => { 'name' => 'Test Foo' } }
+ response.should redirect_to('/account')
+ @user.reload
+ @user.name.should == 'Test Foo'
+ end
+ end
+
+ end
+
+ describe "Without user login" do
+
+ ['show', 'update'].each do |action|
+
+ it "should be redirect with login when visit :#{action}" do
+ get action.to_sym
+
+ response.should redirect_to('/')
+ end
+
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.