1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Can upload images. List is horizontal.

This commit is contained in:
Dan Barber 2014-03-24 22:48:31 +00:00
parent ec0f64938b
commit fd91c67cf7
13 changed files with 108 additions and 1 deletions

View File

@ -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/

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -0,0 +1,5 @@
class HomeController < ApplicationController
def index
redirect_to user_images_path
end
end

View File

@ -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

View File

@ -0,0 +1,2 @@
module HomeHelper
end

View File

@ -1,2 +1,7 @@
class Image < ActiveRecord::Base
dragonfly_accessor :image
validates :image, presence: true
belongs_to :user
end

View File

@ -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

View File

@ -0,0 +1,18 @@
<h1>My Images</h1>
<ul class="images">
<% @images.each do |image| %>
<li>
<%= link_to image_tag(image.image.thumb('100x100#').url), image.image.url %><br>
<%= link_to 'Delete', user_image_path(image), method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
<% end %>
</ul>
<h2>Upload New Image</h2>
<%= simple_form_for [@current_user, @images.new] do |f| %>
<%= f.input :user_id, as: :hidden %>
<%= f.input :image, as: :file %>
<%= f.submit %>
<% end %>

View File

@ -14,7 +14,11 @@
<% end %>
</header>
<main>
<%= yield %>
</main>
</body>
</html>

View File

@ -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'

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe HomeController do
end

View File

@ -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