Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ww create database #63

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.1"

gem "devise"

gem "simple_form"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ GEM
tabulo
awesome_print (1.9.2)
base64 (0.1.1)
bcrypt (3.1.20)
better_errors (2.9.1)
coderay (>= 1.0.0)
erubi (>= 1.0.0)
Expand Down Expand Up @@ -109,6 +110,12 @@ GEM
irb (>= 1.5.0)
reline (>= 0.3.1)
debug_inspector (1.1.0)
devise (4.9.3)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diff-lcs (1.5.0)
diffy (3.4.2)
domain_name (0.5.20190701)
Expand Down Expand Up @@ -215,6 +222,7 @@ GEM
faraday (>= 1, < 3)
sawyer (~> 0.9)
oj (3.13.23)
orm_adapter (0.5.0)
pg (1.4.6)
pry (0.14.2)
coderay (~> 1.1)
Expand Down Expand Up @@ -278,6 +286,9 @@ GEM
regexp_parser (2.8.2)
reline (0.3.9)
io-console (~> 0.5)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
Expand Down Expand Up @@ -357,6 +368,8 @@ GEM
unf_ext
unf_ext (0.0.9.1)
unicode-display_width (2.4.2)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -398,6 +411,7 @@ DEPENDENCIES
bootsnap
capybara
debug
devise
dotenv-rails
draft_matchers
faker
Expand Down
70 changes: 70 additions & 0 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class PhotosController < ApplicationController
before_action :set_photo, only: %i[ show edit update destroy ]

# GET /photos or /photos.json
def index
@photos = Photo.all
end

# GET /photos/1 or /photos/1.json
def show
end

# GET /photos/new
def new
@photo = Photo.new
end

# GET /photos/1/edit
def edit
end

# POST /photos or /photos.json
def create
@photo = Photo.new(photo_params)

respond_to do |format|
if @photo.save
format.html { redirect_to photo_url(@photo), notice: "Photo was successfully created." }
format.json { render :show, status: :created, location: @photo }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /photos/1 or /photos/1.json
def update
respond_to do |format|
if @photo.update(photo_params)
format.html { redirect_to photo_url(@photo), notice: "Photo was successfully updated." }
format.json { render :show, status: :ok, location: @photo }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /photos/1 or /photos/1.json
def destroy
@photo.destroy

respond_to do |format|
format.html { redirect_to photos_url, notice: "Photo was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_photo
@photo = Photo.find(params[:id])
end

# Only allow a list of trusted parameters through.
def photo_params
params.require(:photo).permit(:image, :comments_count, :likes_count, :caption, :owner_id)
end
end
24 changes: 24 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: photos
#
# id :bigint not null, primary key
# caption :text
# comments_count :integer default(0)
# image :string
# likes_count :integer default(0)
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint not null
#
# Indexes
#
# index_photos_on_owner_id (owner_id)
#
# Foreign Keys
#
# fk_rails_... (owner_id => users.id)
#
class Photo < ApplicationRecord
belongs_to :owner, class_name: "User"
end
31 changes: 31 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# comments_count :integer default(0)
# email :citext default(""), not null
# encrypted_password :string default(""), not null
# likes_count :integer default(0)
# private :boolean
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# username :citext
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_username (username) UNIQUE
#
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many :own_photos, class_name: "Photo", foreign_key: "owner_id"
end
42 changes: 42 additions & 0 deletions app/views/photos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%= form_with(model: photo) do |form| %>
<% if photo.errors.any? %>
<div style="color: red">
<h2><%= pluralize(photo.errors.count, "error") %> prohibited this photo from being saved:</h2>

<ul>
<% photo.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :image, style: "display: block" %>
<%= form.text_field :image %>
</div>

<div>
<%= form.label :comments_count, style: "display: block" %>
<%= form.number_field :comments_count %>
</div>

<div>
<%= form.label :likes_count, style: "display: block" %>
<%= form.number_field :likes_count %>
</div>

<div>
<%= form.label :caption, style: "display: block" %>
<%= form.text_area :caption %>
</div>

<div>
<%= form.label :owner_id, style: "display: block" %>
<%= form.text_field :owner_id %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
27 changes: 27 additions & 0 deletions app/views/photos/_photo.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div id="<%= dom_id photo %>">
<p>
<strong>Image:</strong>
<%= photo.image %>
</p>

<p>
<strong>Comments count:</strong>
<%= photo.comments_count %>
</p>

<p>
<strong>Likes count:</strong>
<%= photo.likes_count %>
</p>

<p>
<strong>Caption:</strong>
<%= photo.caption %>
</p>

<p>
<strong>Owner:</strong>
<%= photo.owner_id %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/photos/_photo.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! photo, :id, :image, :comments_count, :likes_count, :caption, :owner_id, :created_at, :updated_at
json.url photo_url(photo, format: :json)
10 changes: 10 additions & 0 deletions app/views/photos/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing photo</h1>

<%= render "form", photo: @photo %>

<br>

<div>
<%= link_to "Show this photo", @photo %> |
<%= link_to "Back to photos", photos_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/photos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Photos</h1>

<div id="photos">
<% @photos.each do |photo| %>
<%= render photo %>
<p>
<%= link_to "Show this photo", photo %>
</p>
<% end %>
</div>

<%= link_to "New photo", new_photo_path %>
1 change: 1 addition & 0 deletions app/views/photos/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @photos, partial: "photos/photo", as: :photo
9 changes: 9 additions & 0 deletions app/views/photos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New photo</h1>

<%= render "form", photo: @photo %>

<br>

<div>
<%= link_to "Back to photos", photos_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/photos/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @photo %>

<div>
<%= link_to "Edit this photo", edit_photo_path(@photo) %> |
<%= link_to "Back to photos", photos_path %>

<%= button_to "Destroy this photo", @photo, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/photos/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "photos/photo", photo: @photo
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "active_support/core_ext/integer/time"

Rails.application.configure do
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
# Allow server to be hosted on any URL
config.hosts.clear
# Allow better_errors to work in online IDE
Expand Down
Loading