diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb new file mode 100644 index 0000000..9afffd7 --- /dev/null +++ b/app/controllers/tags_controller.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class TagsController < ApplicationController + skip_before_action :require_login, only: [:index, :show] + before_action :set_tag, only: [:show] + + def index + @tags = Tag.all + .page(params[:page]) + end + + def show; end + + private + + def set_tag + @tag = Tag.find_by(name: params[:id]) + end +end diff --git a/app/models/tag.rb b/app/models/tag.rb index c15af97..320e815 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -4,4 +4,8 @@ class Tag < ApplicationRecord validates :name, presence: true has_many :microposts_tags has_many :microposts, through: :microposts_tags + + def to_param + name + end end diff --git a/app/views/microposts/_micropost.html.erb b/app/views/microposts/_micropost.html.erb index dbe88a5..987ee63 100644 --- a/app/views/microposts/_micropost.html.erb +++ b/app/views/microposts/_micropost.html.erb @@ -4,7 +4,7 @@
Tags:
<% end %> diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb new file mode 100644 index 0000000..f730143 --- /dev/null +++ b/app/views/tags/index.html.erb @@ -0,0 +1,12 @@ +

<%= t(".tags") %>

+ +<% if @tags.empty? %> +

<%= t(".empty") %>

+<% else %> + + <%= paginate @tags %> +<% end %> diff --git a/app/views/tags/show.html.erb b/app/views/tags/show.html.erb new file mode 100644 index 0000000..a06558a --- /dev/null +++ b/app/views/tags/show.html.erb @@ -0,0 +1,3 @@ +

<%= @tag.name %>

+ +<%= render @tag.microposts %> diff --git a/config/locales/views/tags/en.yml b/config/locales/views/tags/en.yml new file mode 100644 index 0000000..8154023 --- /dev/null +++ b/config/locales/views/tags/en.yml @@ -0,0 +1,6 @@ +en: + tags: + index: + tags: Tags + empty: You don’t have any tags yet + diff --git a/config/routes.rb b/config/routes.rb index c0e4510..4954afe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,4 +14,5 @@ Rails.application.routes.draw do resources :blog_posts resources :microposts + resources :tags, only: [:index, :show] end