From 6358e2a61edf31c6a2a70d6ec4c4528097af1269 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 19 Oct 2023 19:10:20 +0100 Subject: [PATCH] Show tags in microposts --- app/assets/stylesheets/microposts.css | 16 ++++++++++++++++ app/controllers/microposts_controller.rb | 5 +++++ app/views/microposts/_form.html.erb | 2 ++ app/views/microposts/_micropost.html.erb | 8 ++++++++ test/integration/tag_microposts_test.rb | 21 +++++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 test/integration/tag_microposts_test.rb diff --git a/app/assets/stylesheets/microposts.css b/app/assets/stylesheets/microposts.css index c81faa8..edcb4ec 100644 --- a/app/assets/stylesheets/microposts.css +++ b/app/assets/stylesheets/microposts.css @@ -23,6 +23,10 @@ form#micropost_form { } } +ul#microposts { + list-style-type: none; +} + .micropost { margin: 1em auto; padding: .5em; @@ -33,4 +37,16 @@ form#micropost_form { text-align: right; font-size: .8em; } + + h5 { + display: inline; + } + + ul.micropost-tags { + list-style-type: none; + display: inline; + li { + display: inline; + } + } } diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb index 2e4a373..c87e501 100644 --- a/app/controllers/microposts_controller.rb +++ b/app/controllers/microposts_controller.rb @@ -17,6 +17,7 @@ class MicropostsController < ApplicationController def create micropost = Micropost.new(micropost_params) micropost.user = helpers.current_user + micropost.add_tags(params[:tags]) if micropost.save redirect_to micropost, notice: t(".created") else @@ -46,6 +47,10 @@ class MicropostsController < ApplicationController private + def parse_tags(tags) + tags.split + end + def micropost_params params.require(:micropost).permit( :content, diff --git a/app/views/microposts/_form.html.erb b/app/views/microposts/_form.html.erb index aad2958..476dfdf 100644 --- a/app/views/microposts/_form.html.erb +++ b/app/views/microposts/_form.html.erb @@ -3,5 +3,7 @@ <%= f.rich_text_area :content %> + <%= text_field_tag :tags %> + <%= f.submit button_text %> <% end %> diff --git a/app/views/microposts/_micropost.html.erb b/app/views/microposts/_micropost.html.erb index e32326e..dbe88a5 100644 --- a/app/views/microposts/_micropost.html.erb +++ b/app/views/microposts/_micropost.html.erb @@ -1,5 +1,13 @@
<%= micropost.content %> + <% unless micropost.tags.empty? %> +
Tags:
+ + <% end %>
<%= link_to "Edit", edit_micropost_path(micropost) if micropost.user == current_user %> <%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %> diff --git a/test/integration/tag_microposts_test.rb b/test/integration/tag_microposts_test.rb new file mode 100644 index 0000000..5cc3655 --- /dev/null +++ b/test/integration/tag_microposts_test.rb @@ -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