From dbabc3910f77fec9db825289a3b3aa64df9fd3d5 Mon Sep 17 00:00:00 2001 From: Matthew Mongeau Date: Mon, 12 Mar 2012 10:48:44 -0400 Subject: [PATCH] Persist shouts. --- app/controllers/dashboards_controller.rb | 3 +++ app/controllers/shouts_controller.rb | 7 +++++++ app/models/shout.rb | 3 +++ app/models/user.rb | 1 + app/views/dashboards/show.html.erb | 6 ++++++ config/routes.rb | 3 ++- db/migrate/20120312143222_create_shouts.rb | 11 +++++++++++ db/schema.rb | 11 ++++++++++- test/fixtures/shouts.yml | 9 +++++++++ test/unit/shout_test.rb | 7 +++++++ 10 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 app/controllers/shouts_controller.rb create mode 100644 app/models/shout.rb create mode 100644 db/migrate/20120312143222_create_shouts.rb create mode 100644 test/fixtures/shouts.yml create mode 100644 test/unit/shout_test.rb diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index fc2b312..b35146e 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -1,2 +1,5 @@ class DashboardsController < ApplicationController + def show + @shout = Shout.new + end end diff --git a/app/controllers/shouts_controller.rb b/app/controllers/shouts_controller.rb new file mode 100644 index 0000000..499dfd5 --- /dev/null +++ b/app/controllers/shouts_controller.rb @@ -0,0 +1,7 @@ +class ShoutsController < ApplicationController + def create + shout = current_user.shouts.new(params[:shout]) + shout.save + redirect_to dashboard_path, notice: "Shouted!" + end +end diff --git a/app/models/shout.rb b/app/models/shout.rb new file mode 100644 index 0000000..314e3c7 --- /dev/null +++ b/app/models/shout.rb @@ -0,0 +1,3 @@ +class Shout < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 6d077a1..91afa56 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,4 @@ class User < ActiveRecord::Base include Clearance::User + has_many :shouts end diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index 464f78b..a60b598 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -1 +1,7 @@

Dashboard

+Welcome <%= current_user.email %> - <%= link_to "Sign out", sign_out_path, method: :delete %> + +<%= form_for(@shout) do |form| %> + <%= form.text_field :body, placeholder: "Shout here" %> + <%= form.submit "Shout!" %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 6ee5a16..3cfee6a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Shouter::Application.routes.draw do root to: "welcome#index" - resource :dashboard + resource :dashboard, only: [:show] + resources :shouts, only: [:create] end diff --git a/db/migrate/20120312143222_create_shouts.rb b/db/migrate/20120312143222_create_shouts.rb new file mode 100644 index 0000000..05bad3d --- /dev/null +++ b/db/migrate/20120312143222_create_shouts.rb @@ -0,0 +1,11 @@ +class CreateShouts < ActiveRecord::Migration + def change + create_table :shouts do |t| + t.belongs_to :user + t.text :body + + t.timestamps + end + add_index :shouts, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 0427b5c..f862d16 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,16 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120312134910) do +ActiveRecord::Schema.define(:version => 20120312143222) do + + create_table "shouts", :force => true do |t| + t.integer "user_id" + t.text "body" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "shouts", ["user_id"], :name => "index_shouts_on_user_id" create_table "users", :force => true do |t| t.string "email" diff --git a/test/fixtures/shouts.yml b/test/fixtures/shouts.yml new file mode 100644 index 0000000..6afabdd --- /dev/null +++ b/test/fixtures/shouts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + user: + body: MyText + +two: + user: + body: MyText diff --git a/test/unit/shout_test.rb b/test/unit/shout_test.rb new file mode 100644 index 0000000..3b1d5a8 --- /dev/null +++ b/test/unit/shout_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ShoutTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end