From 5fa64127209b3793ca8fd08865837f7345418d8b Mon Sep 17 00:00:00 2001 From: Toru Takahashi Date: Sat, 19 May 2018 01:10:40 +0900 Subject: [PATCH] [phase-6a]change routing of glossary_category to under project --- .../glossary_categories_controller.rb | 19 ++++++++++++++----- app/views/glossary_categories/edit.html.erb | 2 +- app/views/glossary_categories/index.html.erb | 4 ++-- app/views/glossary_categories/new.html.erb | 2 +- app/views/glossary_categories/show.html.erb | 4 ++-- config/routes.rb | 4 ++-- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/controllers/glossary_categories_controller.rb b/app/controllers/glossary_categories_controller.rb index a73b0db..102985f 100644 --- a/app/controllers/glossary_categories_controller.rb +++ b/app/controllers/glossary_categories_controller.rb @@ -1,9 +1,10 @@ class GlossaryCategoriesController < ApplicationController before_action :find_category_from_id, only: [:show, :edit, :update, :destroy] + before_action :find_project_from_id def index - @categories = GlossaryCategory.all + @categories = GlossaryCategory.where(project_id: @project_id) end def show @@ -18,15 +19,16 @@ class GlossaryCategoriesController < ApplicationController def create category = GlossaryCategory.new(glossary_category_params) + category.project = @project if category.save - redirect_to category, notice: l(:notice_successful_create) + redirect_to [@project, category], notice: l(:notice_successful_create) end end def update @category.attributes = glossary_category_params if @category.save - redirect_to @category, notice: l(:notice_successful_update) + redirect_to [@project, @category], notice: l(:notice_successful_update) end rescue ActiveRecord::StaleObjectError flash.now[:error] = l(:notice_locking_conflict) @@ -34,7 +36,7 @@ class GlossaryCategoriesController < ApplicationController def destroy @category.destroy - redirect_to glossary_categories_path + redirect_to project_glossary_categories_path end # Find the category whose id is the :id parameter @@ -45,7 +47,14 @@ class GlossaryCategoriesController < ApplicationController end private - + + # Find the project whose id is the :project_id parameter + def find_project_from_id + @project = Project.find(params[:project_id]) + rescue ActiveRecord::RecordNotFound + render_404 + end + def glossary_category_params params.require(:glossary_category).permit( :name diff --git a/app/views/glossary_categories/edit.html.erb b/app/views/glossary_categories/edit.html.erb index a69491d..1a23b57 100644 --- a/app/views/glossary_categories/edit.html.erb +++ b/app/views/glossary_categories/edit.html.erb @@ -1,7 +1,7 @@

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

<%= labelled_form_for :glossary_category, @category, -url: glossary_category_path do |f| %> +url: project_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 index c2fe0c8..de3e535 100644 --- a/app/views/glossary_categories/index.html.erb +++ b/app/views/glossary_categories/index.html.erb @@ -1,7 +1,7 @@

<%=l :label_glossary_categories %>

- <%= link_to l(:label_glossary_category_new), new_glossary_category_path, class: 'icon icon-add' %> + <%= link_to l(:label_glossary_category_new), new_project_glossary_category_path, class: 'icon icon-add' %>
@@ -15,7 +15,7 @@ <% @categories.each do |category| %> - + <% end %> diff --git a/app/views/glossary_categories/new.html.erb b/app/views/glossary_categories/new.html.erb index 6df492c..8cae7ef 100644 --- a/app/views/glossary_categories/new.html.erb +++ b/app/views/glossary_categories/new.html.erb @@ -1,7 +1,7 @@

<%=l :label_glossary_category_new %>

<%= labelled_form_for :glossary_category, @category, -url: glossary_categories_path do |f| %> +url: project_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 index f5470ec..fca9e16 100644 --- a/app/views/glossary_categories/show.html.erb +++ b/app/views/glossary_categories/show.html.erb @@ -1,6 +1,6 @@
- <%= link_to l(:button_edit), edit_glossary_category_path, class: 'icon icon-edit' %> - <%= link_to l(:button_delete), glossary_category_path, method: :delete, + <%= link_to l(:button_edit), edit_project_glossary_category_path, class: 'icon icon-edit' %> + <%= link_to l(:button_delete), project_glossary_category_path, method: :delete, data: {confirm: l(:text_are_you_sure)}, class: 'icon icon-del' %>
diff --git a/config/routes.rb b/config/routes.rb index ec2e56a..eb14bcf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Rails.application.routes.draw do resources :projects do - resources :glossary_terms + resources :glossary_terms + resources :glossary_categories end - resources :glossary_categories end
<%= category.id %><%= link_to category.name, category %><%= link_to category.name, [@project, category] %>