Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teaching PR #2: Add post editing #4

Open
wants to merge 2 commits into
base: implement_devise
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ body {
.header {
font-size: 20px;
font-weight: bold;
}
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/navigation.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.navigation li {
display: inline;
}
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/post/post_common.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
width: 400px;
padding: 10px;
margin: 10px;
}
}
12 changes: 12 additions & 0 deletions app/assets/stylesheets/post/post_edit.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.post-edit-form textarea {
width: 100%;
}

.post-edit-form .form-group {
overflow: auto;
}

.post-edit-form .form-group .btn {
margin-top: 5px;
float: right;
}
12 changes: 0 additions & 12 deletions app/assets/stylesheets/post/post_new.css.scss

This file was deleted.

10 changes: 10 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def create
redirect_to root_path
end

def edit
@post = Post.find(params[:id])
end

def update
Post.find(params[:id]).update_attributes!(post_params)

redirect_to root_path
end

private
def post_params
params.require(:post).permit(:body)
Expand Down
2 changes: 1 addition & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class Post < ActiveRecord::Base
def friendly_time
created_at.in_time_zone('Eastern Time (US & Canada)').strftime('%F %H:%M')
end
end
end
3 changes: 2 additions & 1 deletion app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
</div>
<div class='post-time italic'><%= post.friendly_time %></div>
</div>
</div>
<div class='post-edit'><%= link_to 'Edit', edit_post_path(post) %></div>
</div>
12 changes: 12 additions & 0 deletions app/views/posts/_post_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class='form-group post-box post-edit-form'>
<%= form_for @post do |f| %>
<div class='post-creator-entry'>
<span>Posting as: <span class='bold'><%= current_user.name %></span></span>
</div>
<div class='post-body-entry'>
<span>Enter your message here:</span>
<span class='entry'><%= f.text_area :body, class: 'form-control' %></span>
</div>
<div class='form-group'><%= f.submit 'Post your message', class: 'btn btn-primary' %></div>
<% end %>
</div>
2 changes: 2 additions & 0 deletions app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<span class='header'>Edit your message</span>
<%= render 'post_form' %>
2 changes: 1 addition & 1 deletion app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<% @posts.each do |post| %>
<%= render partial: 'post', locals: {post: post} %>
<% end %>
<% end %>
<% end %>
13 changes: 1 addition & 12 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
<span class='header'>Submit a new message</span>
<div class='form-group post-box new-post-form'>
<%= form_for @post do |f| %>
<div class='post-creator-entry'>
<span>Posting as: <span class='bold'><%= current_user.name %></span></span>
</div>
<div class='post-body-entry'>
<span>Enter your message here:</span>
<span class='entry'><%= f.text_area :body, class: 'form-control' %></span>
</div>
<div class='form-group'><%= f.submit 'Post your message', class: 'btn btn-primary' %></div>
<% end %>
</div>
<%= render 'post_form' %>