Commit af2a02a

Stephen Peasley <s@stephenpeasley.com>
2015-08-14 01:30:40
Fix profile saving, showing, etc
1 parent cefc5da
app/controllers/profiles_controller.rb
@@ -2,18 +2,22 @@ class ProfilesController < ApplicationController
   
   def show
     @user = User.find_by(username: params[:id])
+    @profile = @user.profile
     @program = Program.stronglifts
   end
   
   def edit
     @user = @current_user
+    @profile = @user.profile
     @program = Program.stronglifts
   end
   
   def update
-    if @current_user.profile.update_attributes(profile_params)
+    if @current_user
+      @profile = @current_user.profile
+      @profile.update_attributes(profile_params)
       flash[:notice] = t("profiles.edit.profile_update_success")
-      redirect_to "/u/#{params[:id]}"
+      redirect_to profile_path(@profile)
     else
       flash[:notice] = t("profiles.edit.profile_updated_error")
       render 'edit'
app/models/profile.rb
@@ -2,4 +2,8 @@ class Profile < ActiveRecord::Base
   belongs_to :user
   enum social_tolerance: { low: 0, medium: 1, high: 2 }
   enum gender: { other: nil,  male: 1, female: 0, transgender: 2 }
+  
+  def to_param
+    user.username
+  end
 end
app/models/user.rb
@@ -58,9 +58,7 @@ class User < ActiveRecord::Base
   private
   
     def create_profile
-      self.profile = Profile.create!(user: self)
-      self.profile.gender = self.profile.social_tolerance = nil
-      self.save!
+      self.profile = Profile.create!(user: self, gender: nil, social_tolerance: nil)
     end
   
 end
app/views/profiles/edit.html.erb
@@ -3,24 +3,24 @@
   <div class="small-12 columns text-center">
     <%= gravatar_for(@user, size: 128) %>
     <h1><%= @user.username %></h1>
-    <%= form_for(:profile, method: :patch) do |f| %>
+    <%= form_for(@profile, method: :patch) do |f| %>
       <fieldset>
         <legend><%= t("profiles.edit.gender.gender") %></legend>
-        <%= radio_button_tag(:gender, "female") %>
-        <%= label_tag(:gender_female, t("profiles.edit.gender.female")) %>
-        <%= radio_button_tag(:gender, "male") %>
-        <%= label_tag(:gender_male, t("profiles.edit.gender.male")) %>
-        <%= radio_button_tag(:gender, "other") %>
-        <%= label_tag(:gender_other, t("profiles.edit.gender.other")) %>
+        <%= f.radio_button(:gender, "female") %>
+        <%= f.label(:gender_female, t("profiles.edit.gender.female")) %>
+        <%= f.radio_button(:gender, "male") %>
+        <%= f.label(:gender_male, t("profiles.edit.gender.male")) %>
+        <%= f.radio_button(:gender, "other") %>
+        <%= f.label(:gender_other, t("profiles.edit.gender.other")) %>
       </fieldset>
       <fieldset>
         <legend><%= t("profiles.edit.social_tolerance.social_tolerance") %></legend>
-        <%= radio_button_tag(:social_tolerance, "low") %>
-        <%= label_tag(:social_tolerance_low, t("profiles.edit.social_tolerance.low")) %>
-        <%= radio_button_tag(:social_tolerance, "medium") %>
-        <%= label_tag(:social_tolerance_medium, t("profiles.edit.social_tolerance.medium")) %>
-        <%= radio_button_tag(:social_tolerance, "high") %>
-        <%= label_tag(:social_tolerance_high, t("profiles.edit.social_tolerance.high")) %>
+        <%= f.radio_button(:social_tolerance, "low") %>
+        <%= f.label(:social_tolerance_low, t("profiles.edit.social_tolerance.low")) %>
+        <%= f.radio_button(:social_tolerance, "medium") %>
+        <%= f.label(:social_tolerance_medium, t("profiles.edit.social_tolerance.medium")) %>
+        <%= f.radio_button(:social_tolerance, "high") %>
+        <%= f.label(:social_tolerance_high, t("profiles.edit.social_tolerance.high")) %>
       </fieldset>
       <%= f.submit t("profiles.edit.save"), class: "button"  %>
     <% end %>
config/routes.rb
@@ -8,9 +8,8 @@ Rails.application.routes.draw do
     end
   end
   resources :programs, only: [:show]
-  resources :profiles, only: [:new, :create, :show, :edit], constraints: { id: /[^\/]+/ }
+  resources :profiles, only: [:new, :create, :show, :edit, :update], constraints: { id: /[^\/]+/ }
   get "/u/:id" => "profiles#show", constraints: { id: /[^\/]+/ }
-  patch "/profiles/:id/edit" => "profiles#update", constraints: { id: /[^\/]+/ }
   get "/dashboard" => "training_sessions#index", as: :dashboard
   get "/terms" => "static_pages#terms"
 end
db/migrate/20150814004716_add_profile_for_each_user.rb
@@ -0,0 +1,10 @@
+class AddProfileForEachUser < ActiveRecord::Migration
+  def up
+    User.find_each do |user|
+      Profile.create!(user: user, gender: nil, social_tolerance: nil) if user.profile.nil?
+    end
+  end
+  
+  def down
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20150616021904) do
+ActiveRecord::Schema.define(version: 20150814004716) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
spec/controllers/profiles_controller_spec.rb
@@ -63,6 +63,7 @@ describe ProfilesController do
         patch :update, id: user.to_param, profile: {gender: "male"}
         user.reload
         expect(user.profile.male?).to be_truthy
+        expect(response).to redirect_to(profile_path(user.profile))
       end
       
     end