Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added editing of profiles

  • Loading branch information...
commit aa9cb08caa1d3d80ccd9eee4e24bd3e01ca5342a 1 parent 34071fe
@ariejan ariejan authored
View
2  CHANGELOG
@@ -2,5 +2,5 @@
================================================================================
* Created initial base_app project
* Added restful_authentication with activation
- * Added profiles, partially, the are not yet editable
+ * Added profiles
* Created default layout
View
35 app/controllers/profiles_controller.rb
@@ -1,26 +1,37 @@
class ProfilesController < ApplicationController
- before_filter :find_user
-
- # TODO: (base_app) enable inline editing of the profile for the owner
+ before_filter :find_profile
+ before_filter :check_owner_access, :only => [:edit, :update]
def show
- @profile = @user.nil? ? nil : @user.profile
+ # render show.html.erb
end
def edit
- redirect_to profile_url(params[:id]) if logged_in? && current_user != @user
+ # render edit.html.erb
end
- def update
- redirect_to profile_url(params[:id]) if logged_in? && current_user != @user
- # TODO: (base_app) Write profile update code
+ def update
+ unless @profile.nil?
+ @profile.update_attributes(params[:profile])
+ flash[:notice] = "Your profile has been succesfully updated."
+ redirect_to profile_url(@profile.user)
+ else
+ render :edit
+ end
end
protected
- def find_user
- @user = User.find(params[:id])
- rescue
- @user = nil
+ def find_profile
+ begin
+ @user = User.find(params[:id])
+ rescue
+ @user = nil
+ end
+ @profile = @user.nil? ? nil : @user.profile
end
+
+ def check_owner_access
+ redirect_to profile_url(params[:id]) if logged_in? && current_user != @user
+ end
end
View
19 app/views/profiles/_my_account.html.erb
@@ -0,0 +1,19 @@
+<% content_for :sidebar do -%>
+<h2>My account</h2>
+
+<ul>
+ <li><%= link_to "Update my profile", edit_profile_url(current_user) %></li>
+</ul>
+
+<ul>
+ <% # TODO: (base_app) Implement 'change email' feature %>
+ <li><%= link_to "Change my email address", "#" %></li>
+ <% # TODO: (base_app) Implement 'change password' feature %>
+ <li><%= link_to "Change my password", "#" %></li>
+</ul>
+
+<ul>
+ <% # TODO: (base_app) Implement 'delete my account' feature %>
+ <li><%= link_to "Delete my account", "#" %></li>
+</ul>
+<% end -%>
View
25 app/views/profiles/edit.html.erb
@@ -0,0 +1,25 @@
+<% if @profile.nil? -%>
+ <% content_for :header do -%>
+ Profile not be found
+ <% end -%>
+
+ <p>The profile you requested does not exist.</p>
+<% else -%>
+ <% content_for :header do -%>
+ Editing my profile
+ <% end -%>
+
+ <%= render :partial => 'profiles/my_account' if current_user == @user %>
+
+ <% form_for :profile, @profile, :url => profile_url(@profile.user), :html => {:method => :put} do |f| %>
+
+ <% @profile.public_attributes.each do |column| -%>
+ <p><label><%= column.humanize %></label><br />
+ <%= input "profile", "#{column}", :value => @profile.send(column) %>
+ </p>
+ <% end -%>
+
+ <%= submit_tag "Save changes" %>
+
+ <% end %>
+<% end -%>
View
22 app/views/profiles/show.html.erb
@@ -9,27 +9,7 @@
Profile for <%= @user.login %>
<% end -%>
- <% if current_user == @user %>
- <% content_for :sidebar do -%>
- <h2>My account</h2>
-
- <ul>
- <li><%= link_to "Update my profile", edit_profile_url(current_user) %></li>
- </ul>
-
- <ul>
- <% # TODO: (base_app) Implement 'change email' feature %>
- <li><%= link_to "Change my email address", "#" %></li>
- <% # TODO: (base_app) Implement 'change password' feature %>
- <li><%= link_to "Change my password", "#" %></li>
- </ul>
-
- <ul>
- <% # TODO: (base_app) Implement 'delete my account' feature %>
- <li><%= link_to "Delete my account", "#" %></li>
- </ul>
- <% end -%>
- <% end %>
+ <%= render :partial => 'profiles/my_account' if current_user == @user %>
<% @profile.public_attributes.each do |column| -%>
<p>
Please sign in to comment.
Something went wrong with that request. Please try again.