Show tags in microposts

This commit is contained in:
Trevor Vallender 2023-10-19 19:10:20 +01:00
parent ad03410a65
commit 6358e2a61e
5 changed files with 52 additions and 0 deletions

View File

@ -23,6 +23,10 @@ form#micropost_form {
} }
} }
ul#microposts {
list-style-type: none;
}
.micropost { .micropost {
margin: 1em auto; margin: 1em auto;
padding: .5em; padding: .5em;
@ -33,4 +37,16 @@ form#micropost_form {
text-align: right; text-align: right;
font-size: .8em; font-size: .8em;
} }
h5 {
display: inline;
}
ul.micropost-tags {
list-style-type: none;
display: inline;
li {
display: inline;
}
}
} }

View File

@ -17,6 +17,7 @@ class MicropostsController < ApplicationController
def create def create
micropost = Micropost.new(micropost_params) micropost = Micropost.new(micropost_params)
micropost.user = helpers.current_user micropost.user = helpers.current_user
micropost.add_tags(params[:tags])
if micropost.save if micropost.save
redirect_to micropost, notice: t(".created") redirect_to micropost, notice: t(".created")
else else
@ -46,6 +47,10 @@ class MicropostsController < ApplicationController
private private
def parse_tags(tags)
tags.split
end
def micropost_params def micropost_params
params.require(:micropost).permit( params.require(:micropost).permit(
:content, :content,

View File

@ -3,5 +3,7 @@
<%= f.rich_text_area :content %> <%= f.rich_text_area :content %>
<%= text_field_tag :tags %>
<%= f.submit button_text %> <%= f.submit button_text %>
<% end %> <% end %>

View File

@ -1,5 +1,13 @@
<div class="micropost"> <div class="micropost">
<%= micropost.content %> <%= micropost.content %>
<% unless micropost.tags.empty? %>
<h5>Tags:</h5>
<ul id="micropost-<%= micropost.id %>-tags" class="micropost-tags">
<% micropost.tags.each do |tag| %>
<li><%= tag.name %></li>
<% end %>
</ul>
<% end %>
<div class="created_at"> <div class="created_at">
<%= link_to "Edit", edit_micropost_path(micropost) if micropost.user == current_user %> <%= link_to "Edit", edit_micropost_path(micropost) if micropost.user == current_user %>
<%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %> <%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %>

View File

@ -0,0 +1,21 @@
require "test_helper"
class TagMicropostsTest < ActionDispatch::IntegrationTest
test "tags can be added to microposts" do
@user = users(:trevor)
sign_in(@user)
get new_micropost_path
assert_response :success
post microposts_path, params: {
micropost: {
content: "An interesting post",
},
tags: "foo bar baz",
}
assert_response :redirect
follow_redirect!
assert_includes response.body, "foo"
assert_includes response.body, "bar"
assert_includes response.body, "baz"
end
end