diff --git a/Gemfile b/Gemfile index 2838fdd..482f7da 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source 'https://rubygems.org' #ruby-gemset=yochigaya gem 'rails', '~> 4.0.0' +gem 'ruby_cowsay' group :development, :test do gem 'sqlite3' diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index c76b925..942883a 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -1,7 +1,13 @@ class StaticPagesController < ApplicationController def home + @cowsay = Cowsay.new end def help end + + def cowsay + words = %w(seito sueyoshi) + @cowsay = Cowsay.new(word: words.sample) + end end diff --git a/app/models/cowsay.rb b/app/models/cowsay.rb new file mode 100644 index 0000000..b9bcc23 --- /dev/null +++ b/app/models/cowsay.rb @@ -0,0 +1,11 @@ +require 'ruby_cowsay' + +class Cowsay + include ActiveModel::Model + + attr_accessor :word + + def exec + Cow.new.say(word) + end +end diff --git a/app/views/static_pages/cowsay.html.erb b/app/views/static_pages/cowsay.html.erb new file mode 100644 index 0000000..5d44b71 --- /dev/null +++ b/app/views/static_pages/cowsay.html.erb @@ -0,0 +1,7 @@ +<%= form_for @cowsay, url:"/static_pages/cowsay" do |f| %> + <%= f.submit value="ボタン" %> +<% end %> + +
+  <%= @cowsay.exec %>
+
diff --git a/app/views/static_pages/home.html.erb b/app/views/static_pages/home.html.erb index 93338e4..d120049 100644 --- a/app/views/static_pages/home.html.erb +++ b/app/views/static_pages/home.html.erb @@ -1,3 +1,3 @@ -
- -
+<%= form_for @cowsay, url:"static_pages/cowsay" do |f| %> + <%= f.submit 'ボタン' %> +<% end -%> diff --git a/config/routes.rb b/config/routes.rb index a55bd23..8dd2332 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Yochigaya::Application.routes.draw do root 'static_pages#home' get 'static_pages/help' + post 'static_pages/cowsay' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes".