From fd91c67cf7495bf7ee154117478c7d0a6275c0de Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Mon, 24 Mar 2014 22:48:31 +0000 Subject: [PATCH] Can upload images. List is horizontal. --- app/assets/javascripts/home.js.coffee | 3 +++ app/assets/stylesheets/home.css.scss | 17 +++++++++++++++++ app/assets/stylesheets/images.css.scss | 8 ++++++++ app/controllers/home_controller.rb | 5 +++++ app/controllers/images_controller.rb | 19 +++++++++++++++++++ app/helpers/home_helper.rb | 2 ++ app/models/image.rb | 5 +++++ app/models/user.rb | 2 ++ app/views/images/index.html.erb | 18 ++++++++++++++++++ app/views/layouts/application.html.erb | 4 ++++ config/routes.rb | 6 +++++- spec/controllers/home_controller_spec.rb | 5 +++++ spec/helpers/home_helper_spec.rb | 15 +++++++++++++++ 13 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/home.js.coffee create mode 100644 app/assets/stylesheets/home.css.scss create mode 100644 app/controllers/home_controller.rb create mode 100644 app/helpers/home_helper.rb create mode 100644 spec/controllers/home_controller_spec.rb create mode 100644 spec/helpers/home_helper_spec.rb diff --git a/app/assets/javascripts/home.js.coffee b/app/assets/javascripts/home.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/home.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/home.css.scss b/app/assets/stylesheets/home.css.scss new file mode 100644 index 0000000..df56f89 --- /dev/null +++ b/app/assets/stylesheets/home.css.scss @@ -0,0 +1,17 @@ +// Place all the styles related to the home controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ + +html { + font-size: 100%; + font-family: 'Helvetica Neue', 'Arial', sans-serif; +} + +header, main { + max-width: 60rem; + margin: 0 auto; +} + +header { + text-align: right; +} diff --git a/app/assets/stylesheets/images.css.scss b/app/assets/stylesheets/images.css.scss index 8d74c21..c55ef10 100644 --- a/app/assets/stylesheets/images.css.scss +++ b/app/assets/stylesheets/images.css.scss @@ -1,3 +1,11 @@ // Place all the styles related to the images controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ + +ul.images { + list-style: none; + padding-left: 0; + li { + display: inline-block; + } +} diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 0000000..4a86fed --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,5 @@ +class HomeController < ApplicationController + def index + redirect_to user_images_path + end +end diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index a6804ff..681fc38 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -2,5 +2,24 @@ class ImagesController < ApplicationController before_action :authenticate_user! def index + @images = @current_user.images + end + + def create + @image = Image.create(permitted_params) + @current_user.images << @image + redirect_to user_images_path + end + + def destroy + image = @current_user.images.find(params[:id]) + image.destroy + redirect_to user_images_path + end + + private + + def permitted_params + params.require(:image).permit(:user_id, :image) end end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb new file mode 100644 index 0000000..23de56a --- /dev/null +++ b/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/app/models/image.rb b/app/models/image.rb index 1b6d3d7..379ff1a 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -1,2 +1,7 @@ class Image < ActiveRecord::Base + dragonfly_accessor :image + + validates :image, presence: true + + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index acbf24a..3745d97 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ class User < ActiveRecord::Base dragonfly_accessor :avatar + has_many :images + def self.find_or_create_from_auth(auth) find_by(auth.slice(:provider, :uid)) || create_from_auth(auth) end diff --git a/app/views/images/index.html.erb b/app/views/images/index.html.erb index e69de29..18a92c7 100644 --- a/app/views/images/index.html.erb +++ b/app/views/images/index.html.erb @@ -0,0 +1,18 @@ +

My Images

+ + + +

Upload New Image

+ +<%= simple_form_for [@current_user, @images.new] do |f| %> + <%= f.input :user_id, as: :hidden %> + <%= f.input :image, as: :file %> + <%= f.submit %> +<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b77195c..c379242 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,7 +14,11 @@ <% end %> +
+ <%= yield %> +
+ diff --git a/config/routes.rb b/config/routes.rb index fa812c1..269dce7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,11 +2,15 @@ MyImages::Application.routes.draw do # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". - root 'images#index' + root 'home#index' resource :session, only: [:new, :destroy] get '/auth/:provider/callback', to: 'sessions#create', as: :create_session + resource :user do + resources :images + end + # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb new file mode 100644 index 0000000..9d48b6a --- /dev/null +++ b/spec/controllers/home_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe HomeController do + +end diff --git a/spec/helpers/home_helper_spec.rb b/spec/helpers/home_helper_spec.rb new file mode 100644 index 0000000..f529425 --- /dev/null +++ b/spec/helpers/home_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the HomeHelper. For example: +# +# describe HomeHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe HomeHelper do + pending "add some examples to (or delete) #{__FILE__}" +end