プロジェクト

全般

プロフィール

« | » 

リビジョン b36e22c9

高徹 高橋 徹 さんが約6年前に追加

[phase-5]add category controller and it's views

差分を表示:

app/controllers/glossary_categories_controller.rb
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
app/helpers/glossary_categories_helper.rb
module GlossaryCategoriesHelper
end
app/views/glossary_categories/_form.html.erb
<div class="box">
<p><%= form.text_field :name, size: 60, required: true %></p>
</div>
app/views/glossary_categories/edit.html.erb
<h2><%=l :label_glossary_category %> $<%= @category.id %></h2>
<%= 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 %>
app/views/glossary_categories/index.html.erb
<h2><%=l :label_glossary_categories %></h2>
<div class="contextual">
<%= link_to l(:label_glossary_category_new), new_glossary_category_path, class: 'icon icon-add' %>
</div>
<table class="list">
<thead>
<tr>
<th>#</th>
<th><%=l :field_name %></th>
</tr>
</thead>
<tbody>
<% @categories.each do |category| %>
<tr>
<td class="id"><%= category.id %></td>
<td class="name"><%= link_to category.name, category %></td>
</tr>
<% end %>
</tbody>
</table>
app/views/glossary_categories/new.html.erb
<h2><%=l :label_glossary_category_new %></h2>
<%= 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 %>
app/views/glossary_categories/show.html.erb
<div class="contextual">
<%= 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' %>
</div>
<h2><%=l :label_glossary_category %> #<%=@category.id %></h2>
<h3><%= @category.name %></h3>
config/locales/en.yml
en:
label_glossary_terms: "Glossary terms"
label_glossary_term: "Glossary term"
label_glossary_term_new: "New glossary term"
label_glossary_term_new: "New glossary term"
label_glossary_categories: "Glossary categories"
label_glossary_category: "Glossary category"
label_glossary_category_new: "New glossary category"
config/locales/ja.yml
ja:
label_glossary_terms: "用語集"
label_glossary_term: "用語"
label_glossary_term_new: "用語の作成"
label_glossary_term_new: "用語の作成"
label_glossary_categories: "用語のカテゴリ"
label_glossary_category: "用語のカテゴリ"
label_glossary_category_new: "カテゴリの作成"
test/functional/glossary_categories_controller_test.rb
require File.expand_path('../../test_helper', __FILE__)
class GlossaryCategoriesControllerTest < ActionController::TestCase
# Replace this with your real tests.
def test_truth
assert true
end
end

他の形式にエクスポート: Unified diff