Commit 0844c47d

mo k <mo@mokhan.ca>
2012-09-06 13:59:40
associate a tutorial with the user signed in.
1 parent 694b2b8
Changed files (3)
app/controllers/tutorials_controller.rb
@@ -1,3 +1,52 @@
-class TutorialsController < InheritedResources::Base
+class TutorialsController < ApplicationController
   before_filter :authenticate_user!, :except => [:show, :index]
+
+  def index
+    @tutorials = Tutorial.all
+  end
+
+  def show
+    @tutorial = Tutorial.find(params[:id])
+  end
+
+  def new
+    @tutorial = Tutorial.new
+  end
+
+  def edit
+    @tutorial = current_user.tutorials.find(params[:id])
+  end
+
+  def create
+    @tutorial = current_user.tutorials.create(params[:tutorial])
+    respond_to do |format|
+      if @tutorial.save
+        format.html { redirect_to( '/tutorials/' ) }
+      else
+        flash[:error] = @tutorial.errors.full_messages
+        format.html { render :action => "new" }
+      end
+    end
+  end
+
+  def update
+    @tutorial = current_user.tutorials.find(params[:id])
+
+    respond_to do |format|
+      if @tutorial.update_attributes(params[:tutorial])
+        format.html { redirect_to(@tutorial, :notice => 'tutorial was successfully updated.') }
+      else
+        format.html { render :action => "edit" }
+      end
+    end
+  end
+
+  def destroy
+    @tutorial = current_user.tutorials.find(params[:id])
+    @tutorial.destroy
+
+    respond_to do |format|
+      format.html { redirect_to(tutorials_url) }
+    end
+  end
 end
app/models/tutorial.rb
@@ -1,3 +1,4 @@
 class Tutorial < ActiveRecord::Base
   attr_accessible :description, :heading, :url
+  belongs_to :user
 end
app/models/user.rb
@@ -6,6 +6,7 @@ class User < ActiveRecord::Base
   attr_accessible :name, :email, :website, :twitter, :facebook, :city, :latitude, :longitude, :password, :password_confirmation, :current_password, :remember_me, :interest_ids
   has_many :creations, :dependent => :destroy
   has_many :favorites, :dependent => :destroy
+  has_many :tutorials, :dependent => :destroy
   has_many :authentications
   has_and_belongs_to_many :interests, :join_table => 'users_interests', :uniq => true, :autosave => true
   has_one :avatar