diff --git a/app/assets/stylesheets/static_pages.css.scss b/app/assets/stylesheets/static_pages.css.scss index 284c9ef..0408814 100644 --- a/app/assets/stylesheets/static_pages.css.scss +++ b/app/assets/stylesheets/static_pages.css.scss @@ -191,4 +191,32 @@ input { border-bottom: 1px solid $grayLighter; } } - } \ No newline at end of file + } + +/* microposts */ + +.microposts { + list-style: none; + margin: 10px 0 0 0; + + li { + padding: 10px 0; + border-top: 1px solid #e8e8e8; + } +} +.content { + display: block; +} +.timestamp { + color: $grayLight; +} +.gravatar { + float: left; + margin-right: 10px; +} +aside { + textarea { + height: 100px; + margin-bottom: 5px; + } +} \ No newline at end of file diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb new file mode 100644 index 0000000..2e65fb0 --- /dev/null +++ b/app/controllers/microposts_controller.rb @@ -0,0 +1,30 @@ +class MicropostsController < ApplicationController + before_action :signed_in_user + + def create + @micropost = current_user.microposts.build(micropost_params) + if @micropost.save + flash[:success] = "Micropost created!" + redirect_to root_url + else + @feed_items = [] + render 'static_pages/home' + end + end + + def destroy + @micropost.destroy + redirect_to root_url + end + + private + + def micropost_params + params.require(:micropost).permit(:content) + end + def correct_user + @micropost = current_user.microposts.find_by(id: params[:id]) + redirect_to root_url if @micropost.nil? + end + +end \ No newline at end of file diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index d0c9ebb..ff86a0f 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -1,5 +1,10 @@ class StaticPagesController < ApplicationController + def home + if signed_in? + @micropost = current_user.microposts.build + @feed_items = current_user.feed.paginate(page: params[:page]) + end end def help diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d86c0ce..6b7b828 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -9,6 +9,7 @@ class UsersController < ApplicationController def show @user = User.find(params[:id]) + @microposts = @user.microposts.paginate(page: params[:page]) end def new @@ -51,13 +52,6 @@ class UsersController < ApplicationController def admin_user redirect_to(root_url) unless current_user.admin? end - - def signed_in_user - unless signed_in? - store_location - redirect_to signin_url, notice: "Please sign in." - end - end def correct_user @user = User.find(params[:id]) diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 3da6641..9559f35 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -25,7 +25,12 @@ module SessionsHelper def current_user?(user) user == current_user end - + def signed_in_user + unless signed_in? + store_location + redirect_to signin_url, notice: "Please sign in." + end + end def sign_out self.current_user = nil diff --git a/app/models/micropost.rb b/app/models/micropost.rb new file mode 100644 index 0000000..70493fe --- /dev/null +++ b/app/models/micropost.rb @@ -0,0 +1,6 @@ +class Micropost < ActiveRecord::Base + belongs_to :user + default_scope -> { order('created_at DESC') } + validates :content, presence: true, length: { maximum: 140 } + validates :user_id, presence: true +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index d68c333..c4128b1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + has_many :microposts, dependent: :destroy before_save { self.email = email.downcase } before_create :create_remember_token validates :name, presence: true, length: { maximum: 50 } @@ -17,6 +18,11 @@ class User < ActiveRecord::Base Digest::SHA1.hexdigest(token.to_s) end + def feed + # This is preliminary. See "Following users" for the full implementation. + Micropost.where("user_id = ?", id) + end + private def create_remember_token diff --git a/app/views/microposts/_micropost.html.erb b/app/views/microposts/_micropost.html.erb new file mode 100644 index 0000000..fe116ba --- /dev/null +++ b/app/views/microposts/_micropost.html.erb @@ -0,0 +1,11 @@ +