From 5c08f7fb0529857c24e6e733ca88636276521aa1 Mon Sep 17 00:00:00 2001 From: Anthony Lewis Date: Sun, 23 Nov 2014 20:57:00 -0600 Subject: [PATCH] Chapter 11 --- app/controllers/text_posts_controller.rb | 14 ++++++++++++++ app/views/text_posts/_text_post.html.erb | 9 ++++++++- app/views/text_posts/edit.html.erb | 5 +++++ app/views/users/show.html.erb | 2 +- config/routes.rb | 2 +- 5 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 app/views/text_posts/edit.html.erb diff --git a/app/controllers/text_posts_controller.rb b/app/controllers/text_posts_controller.rb index e45cd45..ee08ad9 100644 --- a/app/controllers/text_posts_controller.rb +++ b/app/controllers/text_posts_controller.rb @@ -15,6 +15,20 @@ def create end end + def edit + @text_post = current_user.text_posts.find(params[:id]) + end + + def update + @text_post = current_user.text_posts.find(params[:id]) + + if @text_post.update(text_post_params) + redirect_to post_path(@text_post), notice: "Post updated!" + else + render :edit, alert: "Error updating post." + end + end + private def text_post_params diff --git a/app/views/text_posts/_text_post.html.erb b/app/views/text_posts/_text_post.html.erb index 2fb152d..47c5d9d 100644 --- a/app/views/text_posts/_text_post.html.erb +++ b/app/views/text_posts/_text_post.html.erb @@ -8,6 +8,13 @@

By <%= text_post.user.name %>

- <%= text_post.body %> + <%= sanitize text_post.body %> + + <% if text_post.user == current_user %> +

+ <%= link_to 'Edit', edit_text_post_path(text_post), + class: "btn btn-default" %> +

+ <% end %>
diff --git a/app/views/text_posts/edit.html.erb b/app/views/text_posts/edit.html.erb new file mode 100644 index 0000000..d4983c3 --- /dev/null +++ b/app/views/text_posts/edit.html.erb @@ -0,0 +1,5 @@ + + +<%= render 'form' %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 343e615..618f1f0 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -5,7 +5,7 @@

<%= @user.name %>

<%= link_to "Follow", follow_user_path(@user), - class: "btn btn-default" %> + method: :post, class: "btn btn-default" %>

Posts

diff --git a/config/routes.rb b/config/routes.rb index eabd0ef..6841447 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ resources :sessions get 'signup', to: 'users#new', as: 'signup' - get 'follow/:id', to: 'users#follow', as: 'follow_user' + post 'follow/:id', to: 'users#follow', as: 'follow_user' get 'login', to: 'sessions#new', as: 'login' get 'logout', to: 'sessions#destroy', as: 'logout'