Skip to content

Commit 708b200

Browse files
uryelahsarah
and
sarah
authored
Refactor view logic into helpers and partials (#159)
* First refactor on views and creation of comment and user partial * Add wdm gem for windows * Fix comments partial on /posts/show * Change ruby version back to 2.7.0 and remove username and password from database.yml Co-authored-by: sarah <sarachamorro@gmail.com>
1 parent 683dec3 commit 708b200

File tree

8 files changed

+34
-22
lines changed

8 files changed

+34
-22
lines changed

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ gem 'jbuilder', '~> 2.5'
2626
# gem 'redis', '~> 4.0'
2727
# Use ActiveModel has_secure_password
2828
# gem 'bcrypt', '~> 3.1.7'
29-
29+
# Install wdm Gem if the platform is Windows
30+
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
3031
# Use ActiveStorage variant
3132
# gem 'mini_magick', '~> 4.8'
3233

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ GEM
6969
erubi (1.9.0)
7070
execjs (2.7.0)
7171
ffi (1.12.2)
72+
ffi (1.12.2-x64-mingw32)
7273
globalid (0.4.2)
7374
activesupport (>= 4.2.0)
7475
i18n (1.8.2)
@@ -93,14 +94,18 @@ GEM
9394
mini_portile2 (2.4.0)
9495
minitest (5.14.0)
9596
msgpack (1.3.3)
97+
msgpack (1.3.3-x64-mingw32)
9698
nio4r (2.5.2)
9799
nokogiri (1.10.9)
98100
mini_portile2 (~> 2.4.0)
101+
nokogiri (1.10.9-x64-mingw32)
102+
mini_portile2 (~> 2.4.0)
99103
orm_adapter (0.5.0)
100104
parallel (1.19.1)
101105
parser (2.7.0.4)
102106
ast (~> 2.4.0)
103107
pg (1.2.2)
108+
pg (1.2.2-x64-mingw32)
104109
puma (3.12.6)
105110
rack (2.2.3)
106111
rack-test (1.1.0)
@@ -191,11 +196,14 @@ GEM
191196
turbolinks-source (5.2.0)
192197
tzinfo (1.2.6)
193198
thread_safe (~> 0.1)
199+
tzinfo-data (1.2020.1)
200+
tzinfo (>= 1.0.0)
194201
uglifier (4.2.0)
195202
execjs (>= 0.3.0, < 3)
196203
unicode-display_width (1.6.1)
197204
warden (1.2.8)
198205
rack (>= 2.0.6)
206+
wdm (0.1.1)
199207
web-console (3.7.0)
200208
actionview (>= 5.0)
201209
activemodel (>= 5.0)
@@ -207,6 +215,7 @@ GEM
207215

208216
PLATFORMS
209217
ruby
218+
x64-mingw32
210219

211220
DEPENDENCIES
212221
bootsnap (>= 1.1.0)
@@ -226,6 +235,7 @@ DEPENDENCIES
226235
turbolinks (~> 5)
227236
tzinfo-data
228237
uglifier (>= 1.3.0)
238+
wdm (>= 0.1.0)
229239
web-console (>= 3.3.0)
230240

231241
RUBY VERSION

app/helpers/post_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module PostHelper
2+
def display_errors(post)
3+
return unless post.errors.full_messages.any?
4+
5+
content_tag :p, "Post could not be saved. #{post.errors.full_messages.join('. ')}", class: 'errors'
6+
end
7+
end

app/views/comments/_comment.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="post-comments">
2+
<p>
3+
<b><%= comment.user.name %>:</b> <%= comment.content %>
4+
</p>
5+
<span> <%= comment.created_at.strftime("%Y/%m/%d") %> </span>
6+
</div>

app/views/posts/_post.html.erb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313
</p>
1414

1515
<div class='post-comments-section'>
16-
<% post.comments.each do |comment| %>
17-
<div class="post-comments">
18-
<p>
19-
<b><%= comment.user.name %>:</b> <%= comment.content %>
20-
</p>
21-
<span> <%= comment.created_at.strftime("%Y/%m/%d") %> </span>
22-
</div>
23-
<% end %>
16+
<%= render partial: 'comments/comment', collection: post.comments %>
2417

2518
<%= form_for(post.comments.new, url: post_comments_path(post)) do |form| %>
2619
<%= form.text_field :content, id: :comment_content, class: 'form-control', placeholder: 'Add new Comment' %>

app/views/posts/index.html.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<%= form_for(@post) do |form| %>
22
<%= form.label :content %>
33
<%= form.text_field :content, id: :post_content, class: 'form-control', placeholder: 'Add New Post' %>
4-
<% if @post.errors.full_messages.any? %>
5-
<p class="errors">
6-
<%= "Post could not be saved. #{@post.errors.full_messages.join('. ')}" %>
7-
</p>
8-
<% end %>
4+
<%= display_errors(@post) %>
95
<%= form.submit 'Save', class: 'btn btn-primary' %>
106
<% end %>
117

app/views/users/_user.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<li>
2+
Name: <%= user.name %>
3+
<span class="profile-link">
4+
<%= link_to 'See Profile', user_path(user), class: 'profile-link' %>
5+
</span>
6+
</li>

app/views/users/index.html.erb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<section class="users-section">
22
<ul class="users-list">
3-
<% @users.each do |user| %>
4-
<li>
5-
Name: <%= user.name %>
6-
<span class="profile-link">
7-
<%= link_to 'See Profile', user_path(user), class: 'profile-link' %>
8-
</span>
9-
</li>
10-
<% end %>
3+
<%= render @users %>
114
</ul>
125
</section>

0 commit comments

Comments
 (0)