Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

mazes belongs to users

  • Loading branch information...
commit ce62c7188f2887e2cb2bd5752259b8a8feec1d27 1 parent 61e91d7
@KentonWhite authored
View
14 app/controllers/mazes_controller.rb
@@ -3,19 +3,19 @@ class MazesController < ApplicationController
before_filter :login_required
def index
- @mazes = Maze.all
+ @mazes = current_user.mazes.all
end
def show
- @maze = Maze.find(params[:id])
+ @maze = current_user.mazes.find(params[:id])
end
def new
- @maze = Maze.new
+ @maze = current_user.mazes.new
end
def create
- @maze = Maze.new(params[:maze])
+ @maze = current_user.mazes.new(params[:maze])
if @maze.save
flash[:notice] = "Successfully created maze."
redirect_to @maze
@@ -25,11 +25,11 @@ def create
end
def edit
- @maze = Maze.find(params[:id])
+ @maze = current_user.mazes.find(params[:id])
end
def update
- @maze = Maze.find(params[:id])
+ @maze = current_user.mazes.find(params[:id])
if @maze.update_attributes(params[:maze])
flash[:notice] = "Successfully updated maze."
redirect_to @maze
@@ -39,7 +39,7 @@ def update
end
def destroy
- @maze = Maze.find(params[:id])
+ @maze = current_user.mazes.find(params[:id])
@maze.destroy
flash[:notice] = "Successfully destroyed maze."
redirect_to mazes_url
View
3  app/models/maze.rb
@@ -1,3 +1,6 @@
class Maze < ActiveRecord::Base
+
+ belongs_to :maze
+
attr_accessible :title, :user_id
end
View
4 app/models/user.rb
@@ -1,4 +1,6 @@
-class User < ActiveRecord::Base
+class User < ActiveRecord::Base
+
+ has_many :mazes
# new columns need to be added here to be writable through mass assignment
attr_accessible :username, :email, :password, :password_confirmation
View
5 app/views/mazes/_form.html.erb
@@ -4,9 +4,6 @@
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
- <p>
- <%= f.label :user_id %><br />
- <%= f.text_field :user_id %>
- </p>
+
<p><%= f.submit %></p>
<% end %>
View
2  app/views/mazes/index.html.erb
@@ -3,12 +3,10 @@
<table>
<tr>
<th>Title</th>
- <th>User</th>
</tr>
<% for maze in @mazes %>
<tr>
<td><%= maze.title %></td>
- <td><%= maze.user_id %></td>
<td><%= link_to "Show", maze %></td>
<td><%= link_to "Edit", edit_maze_path(maze) %></td>
<td><%= link_to "Destroy", maze, :confirm => 'Are you sure?', :method => :delete %></td>
View
4 app/views/mazes/show.html.erb
@@ -4,10 +4,6 @@
<strong>Title:</strong>
<%= @maze.title %>
</p>
-<p>
- <strong>User:</strong>
- <%= @maze.user_id %>
-</p>
<p>
<%= link_to "Edit", edit_maze_path(@maze) %> |
View
4 config/routes.rb
@@ -5,7 +5,9 @@
match 'logout' => 'sessions#destroy', :as => :logout
- match 'login' => 'sessions#new', :as => :login
+ match 'login' => 'sessions#new', :as => :login
+
+ root :to => 'mazes#index'
resources :sessions
Please sign in to comment.
Something went wrong with that request. Please try again.