View microposts by tag

This commit is contained in:
Trevor Vallender 2023-10-19 19:56:39 +01:00
parent d5b587499c
commit 1d9efa6cab
7 changed files with 46 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -4,7 +4,7 @@
<h5>Tags:</h5>
<ul id="micropost-<%= micropost.id %>-tags" class="micropost-tags">
<% micropost.tags.each do |tag| %>
<li><%= tag.name %></li>
<li><%= link_to tag.name, tag %></li>
<% end %>
</ul>
<% end %>

View File

@ -0,0 +1,12 @@
<h2><%= t(".tags") %></h2>
<% if @tags.empty? %>
<p><%= t(".empty") %></p>
<% else %>
<ul id="tags">
<% @tags.each do |tag| %>
<li><%= link_to tag.name, tag %></li>
<% end %>
</ul>
<%= paginate @tags %>
<% end %>

View File

@ -0,0 +1,3 @@
<h2><%= @tag.name %></h2>
<%= render @tag.microposts %>

View File

@ -0,0 +1,6 @@
en:
tags:
index:
tags: Tags
empty: You dont have any tags yet

View File

@ -14,4 +14,5 @@ Rails.application.routes.draw do
resources :blog_posts
resources :microposts
resources :tags, only: [:index, :show]
end