From b36e22c972e80e9664c2c6670d5d681fb4722294 Mon Sep 17 00:00:00 2001 From: Toru Takahashi Date: Sun, 6 May 2018 17:17:59 +0900 Subject: [PATCH] [phase-5]add category controller and it's views --- .../glossary_categories_controller.rb | 56 +++++++++++++++++++ app/helpers/glossary_categories_helper.rb | 2 + app/views/glossary_categories/_form.html.erb | 3 + app/views/glossary_categories/edit.html.erb | 7 +++ app/views/glossary_categories/index.html.erb | 22 ++++++++ app/views/glossary_categories/new.html.erb | 7 +++ app/views/glossary_categories/show.html.erb | 11 ++++ config/locales/en.yml | 5 +- config/locales/ja.yml | 5 +- .../glossary_categories_controller_test.rb | 8 +++ 10 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 app/controllers/glossary_categories_controller.rb create mode 100644 app/helpers/glossary_categories_helper.rb create mode 100644 app/views/glossary_categories/_form.html.erb create mode 100644 app/views/glossary_categories/edit.html.erb create mode 100644 app/views/glossary_categories/index.html.erb create mode 100644 app/views/glossary_categories/new.html.erb create mode 100644 app/views/glossary_categories/show.html.erb create mode 100644 test/functional/glossary_categories_controller_test.rb diff --git a/app/controllers/glossary_categories_controller.rb b/app/controllers/glossary_categories_controller.rb new file mode 100644 index 0000000..0f0f289 --- /dev/null +++ b/app/controllers/glossary_categories_controller.rb @@ -0,0 +1,56 @@ +class GlossaryCategoriesController < ApplicationController + + before_action :find_category_from_id, only: [:show, :edit, :update, :destroy] + + def index + @categories = GlossaryCategory.all + end + + def show + end + + def new + @category = GlossaryCategory.new + end + + def edit + end + + def create + category = GlossaryCategory.new(glossary_category_params) + if category.save + flash[:notice] = l(:notice_successful_create) + redirect_to category + end + end + + def update + @category.attributes = glossary_category_params + if @category.save + flash[:notice] = l(:notice_successful_update) + redirect_to @category + end + rescue ActiveRecord::StaleObjectError + flash.now[:error] = l(:notice_locking_conflict) + end + + def destroy + @category.destroy + redirect_to glossary_categories_path + end + + # Find the category whose id is the :id parameter + def find_category_from_id + @category = GlossaryCategory.find(params[:id]) + rescue ActiveRecord::RecordNotFound + render_404 + end + + private + + def glossary_category_params + params.require(:glossary_category).permit( + :name + ) + end +end diff --git a/app/helpers/glossary_categories_helper.rb b/app/helpers/glossary_categories_helper.rb new file mode 100644 index 0000000..23a7306 --- /dev/null +++ b/app/helpers/glossary_categories_helper.rb @@ -0,0 +1,2 @@ +module GlossaryCategoriesHelper +end diff --git a/app/views/glossary_categories/_form.html.erb b/app/views/glossary_categories/_form.html.erb new file mode 100644 index 0000000..9d6d0ba --- /dev/null +++ b/app/views/glossary_categories/_form.html.erb @@ -0,0 +1,3 @@ +
+

<%= form.text_field :name, size: 60, required: true %>

+
diff --git a/app/views/glossary_categories/edit.html.erb b/app/views/glossary_categories/edit.html.erb new file mode 100644 index 0000000..a69491d --- /dev/null +++ b/app/views/glossary_categories/edit.html.erb @@ -0,0 +1,7 @@ +

<%=l :label_glossary_category %> $<%= @category.id %>

+ +<%= labelled_form_for :glossary_category, @category, +url: glossary_category_path do |f| %> + <%= render partial: 'glossary_categories/form', locals: {form: f} %> + <%= f.submit l(:button_edit) %> +<% end %> diff --git a/app/views/glossary_categories/index.html.erb b/app/views/glossary_categories/index.html.erb new file mode 100644 index 0000000..c2fe0c8 --- /dev/null +++ b/app/views/glossary_categories/index.html.erb @@ -0,0 +1,22 @@ +

<%=l :label_glossary_categories %>

+ +
+ <%= link_to l(:label_glossary_category_new), new_glossary_category_path, class: 'icon icon-add' %> +
+ + + + + + + + + + <% @categories.each do |category| %> + + + + + <% end %> + +
#<%=l :field_name %>
<%= category.id %><%= link_to category.name, category %>
diff --git a/app/views/glossary_categories/new.html.erb b/app/views/glossary_categories/new.html.erb new file mode 100644 index 0000000..6df492c --- /dev/null +++ b/app/views/glossary_categories/new.html.erb @@ -0,0 +1,7 @@ +

<%=l :label_glossary_category_new %>

+ +<%= labelled_form_for :glossary_category, @category, +url: glossary_categories_path do |f| %> + <%= render partial: 'glossary_categories/form', locals: {form: f} %> + <%= f.submit l(:button_create) %> +<% end %> diff --git a/app/views/glossary_categories/show.html.erb b/app/views/glossary_categories/show.html.erb new file mode 100644 index 0000000..f5470ec --- /dev/null +++ b/app/views/glossary_categories/show.html.erb @@ -0,0 +1,11 @@ +
+ <%= link_to l(:button_edit), edit_glossary_category_path, class: 'icon icon-edit' %> + <%= link_to l(:button_delete), glossary_category_path, method: :delete, + data: {confirm: l(:text_are_you_sure)}, class: 'icon icon-del' %> +
+ +

<%=l :label_glossary_category %> #<%=@category.id %>

+ +

<%= @category.name %>

+ + diff --git a/config/locales/en.yml b/config/locales/en.yml index c403bbd..adb138d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,4 +2,7 @@ en: label_glossary_terms: "Glossary terms" label_glossary_term: "Glossary term" - label_glossary_term_new: "New glossary term" \ No newline at end of file + label_glossary_term_new: "New glossary term" + label_glossary_categories: "Glossary categories" + label_glossary_category: "Glossary category" + label_glossary_category_new: "New glossary category" \ No newline at end of file diff --git a/config/locales/ja.yml b/config/locales/ja.yml index e3dcb96..7b1ab05 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -1,4 +1,7 @@ ja: label_glossary_terms: "用語集" label_glossary_term: "用語" - label_glossary_term_new: "用語の作成" \ No newline at end of file + label_glossary_term_new: "用語の作成" + label_glossary_categories: "用語のカテゴリ" + label_glossary_category: "用語のカテゴリ" + label_glossary_category_new: "カテゴリの作成" diff --git a/test/functional/glossary_categories_controller_test.rb b/test/functional/glossary_categories_controller_test.rb new file mode 100644 index 0000000..b5d8605 --- /dev/null +++ b/test/functional/glossary_categories_controller_test.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../test_helper', __FILE__) + +class GlossaryCategoriesControllerTest < ActionController::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end