Skip to content

Commit

Permalink
thru 10.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Milhouse committed Feb 27, 2012
1 parent e80fd1f commit 0e2bf8a
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 19 deletions.
16 changes: 16 additions & 0 deletions app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class MicropostsController < ApplicationController
before_filter :signed_in_user

def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_path
else
render 'static_pages/home'
end
end

def destroy
end
end
1 change: 1 addition & 0 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class StaticPagesController < ApplicationController
def home
@micropost = current_user.microposts.build if signed_in?
end

def help
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ def destroy
end

private

def signed_in_user
store_location
redirect_to signin_path, notice: "Please sign in." unless signed_in?
end

def correct_user
@user = User.find(params[:id])
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def current_user?(user)
user == current_user
end

def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end

def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
Expand Down
9 changes: 5 additions & 4 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<% if @user.errors.any? %>
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:</h2>
<h2><%= pluralize(object.errors.count, "error") %>
prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
Expand Down
9 changes: 9 additions & 0 deletions app/views/shared/_micropost_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
11 changes: 11 additions & 0 deletions app/views/shared/_user_info.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="user_info">
<a href="<%= user_path(current_user) %>">
<%= gravatar_for(current_user, size: 30) %>
<span class="user_name">
<%= current_user.name %>
</span>
<span class="microposts">
<%= pluralize(current_user.microposts.count, "micropost") %>
</span>
</a>
</div>
30 changes: 22 additions & 8 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<<% provide(:title, 'Home') %>
<% if signed_in? %>
<table class="front">
<tr>
<td class="main">
<h1 class="micropost">What's up?</h1>
<%= render 'shared/micropost_form' %>
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
</td>
</tr>
</table>
<% else %>
<h1>Sample App</h1>

<p>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</p>
<p>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</p>

<%= link_to "Sign up now!", signup_path, class: "signup_button round" %>
<%= link_to "Sign up now!", signup_path, class: "signup_button round" %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>Edit user</h1>

<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]

match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
Expand Down

0 comments on commit 0e2bf8a

Please sign in to comment.