Skip to content
Browse files

User has_many Posts

  • Loading branch information...
1 parent 74ebd04 commit 0b3ad2fa2188d1ad87ebeff3cb0606173364f605 @jandudulski committed Dec 27, 2011
View
1 app/controllers/posts_controller.rb
@@ -41,6 +41,7 @@ def edit
# POST /posts.json
def create
@post = Post.new(params[:post])
+ @post.user = current_user
respond_to do |format|
if @post.save
View
3 app/controllers/users/sessions_controller.rb
@@ -1,7 +1,4 @@
class Users::SessionsController < ApplicationController
def new
end
-
- def destroy
- end
end
View
1 app/models/post.rb
@@ -1,2 +1,3 @@
class Post < ActiveRecord::Base
+ belongs_to :user
end
View
2 app/models/user.rb
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_many :posts, dependent: :destroy
+
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :omniauthable, :rememberable, :trackable
View
8 app/views/layouts/application.html.erb
@@ -13,8 +13,12 @@
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
- <p class="sign_in">
- <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
+ <p>
+ <% if user_signed_in? %>
+ <%= link_to 'Sign out', destroy_user_session_path, method: :delete %>
+ <% else %>
+ <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %><br/>
+ <% end %>
</p>
</header>
<div class="content">
View
5 config/routes.rb
@@ -1,7 +1,10 @@
Dajpieroga::Application.routes.draw do
resources :posts
- devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
+ devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } do
+ get 'sign_in', to: 'users/sessions#new', as: :new_user_session
+ delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
+ end
# The priority is based upon order of creation:
# first created -> highest priority.
View
5 db/migrate/20111227141400_add_user_id_to_posts.rb
@@ -0,0 +1,5 @@
+class AddUserIdToPosts < ActiveRecord::Migration
+ def change
+ add_column :posts, :user_id, :integer
+ end
+end
View
3 db/schema.rb
@@ -11,12 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111227131331) do
+ActiveRecord::Schema.define(:version => 20111227141400) do
create_table "posts", :force => true do |t|
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.integer "user_id"
end
create_table "users", :force => true do |t|

0 comments on commit 0b3ad2f

Please sign in to comment.
Something went wrong with that request. Please try again.