Permalink
Browse files

created user_widget relationship

  • Loading branch information...
1 parent 74a0e6c commit f3d58c1f7a6698f19a706464b44111ffe1473f57 @philmill committed Jun 22, 2011
@@ -1,8 +1,10 @@
class WidgetsController < ApplicationController
+ before_filter :grab_user_from_user_id
+
# GET /widgets
# GET /widgets.xml
def index
- @widgets = Widget.all
+ @widgets = @user.widgets.all
respond_to do |format|
format.html # index.html.erb
@@ -13,7 +15,7 @@ def index
# GET /widgets/1
# GET /widgets/1.xml
def show
- @widget = Widget.find(params[:id])
+ @widget = @user.widgets.find(params[:id])
respond_to do |format|
format.html # show.html.erb
@@ -24,7 +26,7 @@ def show
# GET /widgets/new
# GET /widgets/new.xml
def new
- @widget = Widget.new
+ @widget = @user.widgets.new
respond_to do |format|
format.html # new.html.erb
@@ -34,17 +36,17 @@ def new
# GET /widgets/1/edit
def edit
- @widget = Widget.find(params[:id])
+ @widget = @user.widgets.find(params[:id])
end
# POST /widgets
# POST /widgets.xml
def create
- @widget = Widget.new(params[:widget])
+ @widget = @user.widgets.new(params[:widget])
respond_to do |format|
if @widget.save
- format.html { redirect_to(@widget, :notice => 'Widget was successfully created.') }
+ format.html { redirect_to([@user, @widget], :notice => 'Widget was successfully created.') }
format.xml { render :xml => @widget, :status => :created, :location => @widget }
else
format.html { render :action => "new" }
@@ -56,11 +58,11 @@ def create
# PUT /widgets/1
# PUT /widgets/1.xml
def update
- @widget = Widget.find(params[:id])
+ @widget = @user.widgets.find(params[:id])
respond_to do |format|
if @widget.update_attributes(params[:widget])
- format.html { redirect_to(@widget, :notice => 'Widget was successfully updated.') }
+ format.html { redirect_to([@user, @widget], :notice => 'Widget was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
@@ -72,12 +74,18 @@ def update
# DELETE /widgets/1
# DELETE /widgets/1.xml
def destroy
- @widget = Widget.find(params[:id])
- @widget.destroy
+ Widget.find(params[:id]).destroy
respond_to do |format|
- format.html { redirect_to(widgets_url) }
+ format.html { redirect_to(user_widgets_url(@user)) }
format.xml { head :ok }
end
end
+
+ private
+
+ def grab_user_from_user_id
+ @user = User.find(params[:user_id])
+ end
end
+
View
@@ -1,2 +1,4 @@
class User < ActiveRecord::Base
+ has_many :widgets
end
+
View
@@ -1,2 +1,4 @@
class Widget < ActiveRecord::Base
+ belongs_to :user
end
+
@@ -6,6 +6,7 @@
<th></th>
<th></th>
<th></th>
+ <th></th>
</tr>
<% @users.each do |user| %>
@@ -14,10 +15,12 @@
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
+ <td><%= link_to 'Widgets', user_widgets_path(user) %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
+
@@ -1,4 +1,4 @@
-<%= form_for(@widget) do |f| %>
+<%= form_for([@user, @widget]) do |f| %>
<% if @widget.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@widget.errors.count, "error") %> prohibited this widget from being saved:</h2>
@@ -23,3 +23,4 @@
<%= f.submit %>
</div>
<% end %>
+
@@ -2,5 +2,6 @@
<%= render 'form' %>
-<%= link_to 'Show', @widget %> |
-<%= link_to 'Back', widgets_path %>
+<%= link_to 'Show', [@user, @widget] %> |
+<%= link_to 'Back', user_widgets_path(@user) %>
+
@@ -13,13 +13,15 @@
<tr>
<td><%= widget.title %></td>
<td><%= widget.description %></td>
- <td><%= link_to 'Show', widget %></td>
- <td><%= link_to 'Edit', edit_widget_path(widget) %></td>
- <td><%= link_to 'Destroy', widget, :confirm => 'Are you sure?', :method => :delete %></td>
+ <td><%= link_to 'Show', user_widget_path(@user,widget) %></td>
+ <td><%= link_to 'Edit', edit_user_widget_path(@user, widget) %></td>
+ <td><%= link_to 'Destroy', user_widget_path(@user, widget), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
-<%= link_to 'New Widget', new_widget_path %>
+<%= link_to 'New Widget', new_user_widget_path(@user) %>
+<%= link_to 'Back', users_path %>
+
@@ -2,4 +2,5 @@
<%= render 'form' %>
-<%= link_to 'Back', widgets_path %>
+<%= link_to 'Back', user_widgets_path(@user) %>
+
@@ -11,5 +11,6 @@
</p>
-<%= link_to 'Edit', edit_widget_path(@widget) %> |
-<%= link_to 'Back', widgets_path %>
+<%= link_to 'Edit', edit_user_widget_path(@widget) %> |
+<%= link_to 'Back', user_widgets_path(@user) %>
+
View
@@ -1,7 +1,10 @@
SweeperTest::Application.routes.draw do
- resources :widgets
- resources :users
+ resources :users do
+ resources :widgets
+ end
+
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,9 @@
+class AddUserIdToWidgets < ActiveRecord::Migration
+ def self.up
+ add_column :widgets, :user_id, :integer
+ end
+
+ def self.down
+ remove_column :widgets, :user_id
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110621223009) do
+ActiveRecord::Schema.define(:version => 20110621223403) do
create_table "users", :force => true do |t|
t.string "name"
@@ -23,6 +23,7 @@
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "user_id"
end
end

0 comments on commit f3d58c1

Please sign in to comment.