View microposts by tag
This commit is contained in:
parent
d5b587499c
commit
1d9efa6cab
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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 %>
|
||||
|
|
|
@ -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 %>
|
|
@ -0,0 +1,3 @@
|
|||
<h2><%= @tag.name %></h2>
|
||||
|
||||
<%= render @tag.microposts %>
|
|
@ -0,0 +1,6 @@
|
|||
en:
|
||||
tags:
|
||||
index:
|
||||
tags: Tags
|
||||
empty: You don’t have any tags yet
|
||||
|
|
@ -14,4 +14,5 @@ Rails.application.routes.draw do
|
|||
|
||||
resources :blog_posts
|
||||
resources :microposts
|
||||
resources :tags, only: [:index, :show]
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue