Skip to content

Commit 3c5d6f9

Browse files
committed
add controllers for Continent, Country, Product, Reason
1 parent 52ed64d commit 3c5d6f9

37 files changed

+886
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class ContinentsController < ApplicationController
2+
def index
3+
if params[:code]
4+
@continents = Continent.find_by_code(params[:code])
5+
else
6+
@continents = Continent.all
7+
end
8+
9+
respond_to do |format|
10+
format.html
11+
format.json { render json: @continents.to_json(:only => [:code, :name]) }
12+
end
13+
end
14+
15+
def show
16+
@continent = Continent.find(params[:id])
17+
end
18+
19+
def new
20+
@continent = Continent.new
21+
end
22+
23+
def create
24+
@continent = Continent.new(params[:continent])
25+
if @continent.save
26+
redirect_to @continent, :notice => "Successfully created continent."
27+
else
28+
render :action => 'new'
29+
end
30+
end
31+
32+
def edit
33+
@continent = Continent.find(params[:id])
34+
end
35+
36+
def update
37+
@continent = Continent.find(params[:id])
38+
if @continent.update_attributes(params[:continent])
39+
redirect_to @continent, :notice => "Successfully updated continent."
40+
else
41+
render :action => 'edit'
42+
end
43+
end
44+
45+
def destroy
46+
@continent = Continent.find(params[:id])
47+
@continent.destroy
48+
redirect_to continents_url, :notice => "Successfully destroyed continent."
49+
end
50+
end
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class CountriesController < ApplicationController
2+
def index
3+
if params[:code]
4+
@countries = Country.find_by_code(params[:code])
5+
else
6+
@countries = Country.all
7+
end
8+
9+
respond_to do |format|
10+
format.html
11+
format.json { render json: @countries.to_json(:only => [:code, :name]) }
12+
end
13+
end
14+
15+
def show
16+
@country = Country.find(params[:id])
17+
end
18+
19+
def new
20+
@country = Country.new
21+
end
22+
23+
def create
24+
@country = Country.new(params[:country])
25+
if @country.save
26+
redirect_to @country, :notice => "Successfully created country."
27+
else
28+
render :action => 'new'
29+
end
30+
end
31+
32+
def edit
33+
@country = Country.find(params[:id])
34+
end
35+
36+
def update
37+
@country = Country.find(params[:id])
38+
if @country.update_attributes(params[:country])
39+
redirect_to @country, :notice => "Successfully updated country."
40+
else
41+
render :action => 'edit'
42+
end
43+
end
44+
45+
def destroy
46+
@country = Country.find(params[:id])
47+
@country.destroy
48+
redirect_to countries_url, :notice => "Successfully destroyed country."
49+
end
50+
end
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class ProductsController < ApplicationController
2+
3+
def index
4+
@products = Product.all
5+
6+
respond_to do |format|
7+
format.html
8+
format.json { render json: @products.to_json(:only => [:id, :name]) }
9+
end
10+
end
11+
12+
def show
13+
@product = Product.find(params[:id])
14+
end
15+
16+
def new
17+
@product = Product.new
18+
end
19+
20+
def create
21+
@product = Product.new(params[:product])
22+
if @product.save
23+
redirect_to @product, :notice => "Successfully created product."
24+
else
25+
render :action => 'new'
26+
end
27+
end
28+
29+
def edit
30+
@product = Product.find(params[:id])
31+
end
32+
33+
def update
34+
@product = Product.find(params[:id])
35+
if @product.update_attributes(params[:product])
36+
redirect_to @product, :notice => "Successfully updated product."
37+
else
38+
render :action => 'edit'
39+
end
40+
end
41+
42+
def destroy
43+
@product = Product.find(params[:id])
44+
@product.destroy
45+
redirect_to products_url, :notice => "Successfully destroyed product."
46+
end
47+
end

app/controllers/reasons_controller.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class ReasonsController < ApplicationController
2+
def index
3+
@reasons = Reason.all
4+
5+
respond_to do |format|
6+
format.html
7+
format.json { render json: @reasons.to_json(:only => [:id, :name]) }
8+
end
9+
end
10+
11+
def show
12+
@reason = Reason.find(params[:id])
13+
end
14+
15+
def new
16+
@reason = Reason.new
17+
end
18+
19+
def create
20+
@reason = Reason.new(params[:reason])
21+
if @reason.save
22+
redirect_to @reason, :notice => "Successfully created reason."
23+
else
24+
render :action => 'new'
25+
end
26+
end
27+
28+
def edit
29+
@reason = Reason.find(params[:id])
30+
end
31+
32+
def update
33+
@reason = Reason.find(params[:id])
34+
if @reason.update_attributes(params[:reason])
35+
redirect_to @reason, :notice => "Successfully updated reason."
36+
else
37+
render :action => 'edit'
38+
end
39+
end
40+
41+
def destroy
42+
@reason = Reason.find(params[:id])
43+
@reason.destroy
44+
redirect_to reasons_url, :notice => "Successfully destroyed reason."
45+
end
46+
end

app/helpers/continents_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ContinentsHelper
2+
end

app/helpers/countries_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module CountriesHelper
2+
end

app/helpers/error_messages_helper.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module ErrorMessagesHelper
2+
# Render error messages for the given objects. The :message and :header_message options are allowed.
3+
def error_messages_for(*objects)
4+
options = objects.extract_options!
5+
options[:header_message] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields")
6+
options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "Correct the following errors and try again.")
7+
messages = objects.compact.map { |o| o.errors.full_messages }.flatten
8+
unless messages.empty?
9+
content_tag(:div, :class => "error_messages") do
10+
list_items = messages.map { |msg| content_tag(:li, msg) }
11+
content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
12+
end
13+
end
14+
end
15+
16+
module FormBuilderAdditions
17+
def error_messages(options = {})
18+
@template.error_messages_for(@object, options)
19+
end
20+
end
21+
end
22+
23+
ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)

app/helpers/layout_helper.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# These helper methods can be called in your template to set variables to be used in the layout
2+
# This module should be included in all views globally,
3+
# to do so you may need to add this line to your ApplicationController
4+
# helper :layout
5+
module LayoutHelper
6+
def title(page_title, show_title = true)
7+
content_for(:title) { h(page_title.to_s) }
8+
@show_title = show_title
9+
end
10+
11+
def show_title?
12+
@show_title
13+
end
14+
15+
def stylesheet(*args)
16+
content_for(:head) { stylesheet_link_tag(*args) }
17+
end
18+
19+
def javascript(*args)
20+
content_for(:head) { javascript_include_tag(*args) }
21+
end
22+
end

app/helpers/products_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ProductsHelper
2+
end

app/helpers/reasons_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ReasonsHelper
2+
end

app/views/continents/_form.html.haml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
= form_for @continent do |f|
2+
= f.error_messages
3+
%p
4+
= f.label :code
5+
%br
6+
= f.text_field :code
7+
%p
8+
= f.label :name
9+
%br
10+
= f.text_field :name
11+
%p
12+
= f.submit

app/views/continents/edit.html.haml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- title "Edit Continent"
2+
3+
= render 'form'
4+
5+
%p
6+
= link_to "Show", @continent
7+
|
8+
= link_to "View All", continents_path

app/views/continents/index.html.haml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- title "Continents"
2+
3+
%table
4+
%tr
5+
%th Code
6+
%th Name
7+
- for continent in @continents
8+
%tr
9+
%td= continent.code
10+
%td= continent.name
11+
%td= link_to 'Show', continent
12+
%td= link_to 'Edit', edit_continent_path(continent)
13+
%td= link_to 'Destroy', continent, :confirm => 'Are you sure?', :method => :delete
14+
15+
%p= link_to "New Continent", new_continent_path

app/views/continents/new.html.haml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- title "New Continent"
2+
3+
= render 'form'
4+
5+
%p= link_to "Back to List", continents_path

app/views/continents/show.html.haml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- title "Continent"
2+
3+
%p
4+
%strong Code:
5+
= @continent.code
6+
%p
7+
%strong Name:
8+
= @continent.name
9+
10+
%p
11+
= link_to "Edit", edit_continent_path(@continent)
12+
|
13+
= link_to "Destroy", @continent, :confirm => 'Are you sure?', :method => :delete
14+
|
15+
= link_to "View All", continents_path

app/views/countries/_form.html.haml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
= form_for @country do |f|
2+
= f.error_messages
3+
%p
4+
= f.label :continent_id
5+
%br
6+
= f.number_field :continent_id
7+
%p
8+
= f.label :code
9+
%br
10+
= f.text_field :code
11+
%p
12+
= f.label :name
13+
%br
14+
= f.text_field :name
15+
%p
16+
= f.label :isoNumeric
17+
%br
18+
= f.number_field :isoNumeric
19+
%p
20+
= f.label :isoAlpha3
21+
%br
22+
= f.text_field :isoAlpha3
23+
%p
24+
= f.label :fipsCode
25+
%br
26+
= f.text_field :fipsCode
27+
%p
28+
= f.label :capital
29+
%br
30+
= f.text_field :capital
31+
%p
32+
= f.label :areaInSqKm
33+
%br
34+
= f.text_field :areaInSqKm
35+
%p
36+
= f.label :population
37+
%br
38+
= f.number_field :population
39+
%p
40+
= f.label :currencyCode
41+
%br
42+
= f.text_field :currencyCode
43+
%p
44+
= f.label :languages
45+
%br
46+
= f.text_field :languages
47+
%p
48+
= f.label :geonameId
49+
%br
50+
= f.number_field :geonameId
51+
%p
52+
= f.label :bBoxWest
53+
%br
54+
= f.text_field :bBoxWest
55+
%p
56+
= f.label :bBoxNorth
57+
%br
58+
= f.text_field :bBoxNorth
59+
%p
60+
= f.label :bBoxEast
61+
%br
62+
= f.text_field :bBoxEast
63+
%p
64+
= f.label :bBoxSouth
65+
%br
66+
= f.text_field :bBoxSouth
67+
%p
68+
= f.submit

app/views/countries/edit.html.haml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- title "Edit Country"
2+
3+
= render 'form'
4+
5+
%p
6+
= link_to "Show", @country
7+
|
8+
= link_to "View All", countries_path

0 commit comments

Comments
 (0)