diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb new file mode 100644 index 0000000..fdbfab5 --- /dev/null +++ b/app/controllers/tags_controller.rb @@ -0,0 +1,48 @@ +class TagsController < ApplicationController + allow_unauthenticated_access only: %i[ index show ] + before_action :set_tag, only: %i[ show edit update destroy ] + def index + @tags = Tag.all + end + + def show + end + + def new + @tag = Tag.new + end + + def create + @tag = Tag.new(tag_params) + if @tag.save + redirect_to @tag + else + render :new, status: :unprocessable_entity + end + end + + def edit + end + + def update + if @tag.update(tag_params) + redirect_to @tag + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @tag.destroy + redirect_to tags_path + end + + private + def set_tag + @tag = Tag.find(params[:id]) + end + + def tag_params + params.expect(tag: [ :name ]) + end +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index fab874b..b6ce16f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -34,6 +34,7 @@ <%= link_to "Login", new_session_path unless authenticated? %> <%= link_to "Clips", clips_path %> <%= link_to "Categories", categories_path %> + <%= link_to "Tags", tags_path %>
<%= yield %> diff --git a/app/views/tags/_form.html.erb b/app/views/tags/_form.html.erb new file mode 100644 index 0000000..ba9f9ab --- /dev/null +++ b/app/views/tags/_form.html.erb @@ -0,0 +1,10 @@ +<%= form_with model: tag do |form| %> +
+ <%= form.label :name %> + <%= form.text_field :name %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/tags/edit.html.erb b/app/views/tags/edit.html.erb new file mode 100644 index 0000000..1e64f79 --- /dev/null +++ b/app/views/tags/edit.html.erb @@ -0,0 +1,4 @@ +

Edit tag

+ +<%= render "form", tag: @tag %> +<%= link_to "Cancel", @tag %> \ No newline at end of file diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb new file mode 100644 index 0000000..b1b42c8 --- /dev/null +++ b/app/views/tags/index.html.erb @@ -0,0 +1,9 @@ +

Tags

+ +<%= link_to "New tag", new_tag_path if authenticated? %> + + \ No newline at end of file diff --git a/app/views/tags/new.html.erb b/app/views/tags/new.html.erb new file mode 100644 index 0000000..f27e4e9 --- /dev/null +++ b/app/views/tags/new.html.erb @@ -0,0 +1,4 @@ +

New tag

+ +<%= render "form", tag: @tag %> +<%= link_to "Cancel", tags_path %> \ No newline at end of file diff --git a/app/views/tags/show.html.erb b/app/views/tags/show.html.erb new file mode 100644 index 0000000..e42470d --- /dev/null +++ b/app/views/tags/show.html.erb @@ -0,0 +1,9 @@ +<% cache @tag do %> +

<%= @tag.name %>

+<% end %> + +<%= link_to "Back", tags_path%> +<% if authenticated? %> + <%= link_to "Edit", edit_tag_path(@tag) %> + <%= button_to "Delete", @tag, method: :delete, data: { turbo_confirm: "Are you sure?" } %> +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c6a7087..84ee6ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ Rails.application.routes.draw do resources :clips resources :categories + resources :tags root "home#index" end