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:
parent
ec0f64938b
commit
fd91c67cf7
3
app/assets/javascripts/home.js.coffee
Normal file
3
app/assets/javascripts/home.js.coffee
Normal 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/
|
||||
17
app/assets/stylesheets/home.css.scss
Normal file
17
app/assets/stylesheets/home.css.scss
Normal 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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
5
app/controllers/home_controller.rb
Normal file
5
app/controllers/home_controller.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class HomeController < ApplicationController
|
||||
def index
|
||||
redirect_to user_images_path
|
||||
end
|
||||
end
|
||||
@ -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
|
||||
|
||||
2
app/helpers/home_helper.rb
Normal file
2
app/helpers/home_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module HomeHelper
|
||||
end
|
||||
@ -1,2 +1,7 @@
|
||||
class Image < ActiveRecord::Base
|
||||
dragonfly_accessor :image
|
||||
|
||||
validates :image, presence: true
|
||||
|
||||
belongs_to :user
|
||||
end
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 %>
|
||||
@ -14,7 +14,11 @@
|
||||
<% end %>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</main>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -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'
|
||||
|
||||
|
||||
5
spec/controllers/home_controller_spec.rb
Normal file
5
spec/controllers/home_controller_spec.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe HomeController do
|
||||
|
||||
end
|
||||
15
spec/helpers/home_helper_spec.rb
Normal file
15
spec/helpers/home_helper_spec.rb
Normal 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
|
||||
Loading…
Reference in New Issue
Block a user