Skip to content

Commit

Permalink
validation and linked profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
devpost-mzheng committed Nov 23, 2020
1 parent 68d49df commit 8f1ec9b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gem 'jbuilder', '~> 2.5'
# gem 'capistrano-rails', group: :development

gem 'simple_form'
gem "validate_url"

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ GEM
thread_safe (~> 0.1)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
validate_url (1.0.13)
activemodel (>= 3.0.0)
public_suffix
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand Down Expand Up @@ -212,6 +215,7 @@ DEPENDENCIES
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
validate_url
web-console (>= 3.3.0)

RUBY VERSION
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/linked_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class LinkedProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]

# GET /linked_profiles
def index
@profiles = Profile.joins(:links)
end
end
6 changes: 1 addition & 5 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ def update
end

# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
redirect_to profiles_url, notice: 'Profile was successfully destroyed.'
end

private
Expand Down
3 changes: 3 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Link < ApplicationRecord
belongs_to :profile

validates :name, :url, presence: true
validates :url, url: true
end
3 changes: 3 additions & 0 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Profile < ApplicationRecord
has_many :links

validates :name, presence: true
validates :age, numericality: { only_integer: true }
end
38 changes: 38 additions & 0 deletions app/views/linked_profiles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<p id="notice"><%= notice %></p>

<h1>Profiles</h1>

<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @profiles.each do |profile| %>
<% cache profile do %>
<tr>
<td><b>Name: <%= profile.name %></b></td>
</tr>
<tr>
<td>Age: <%= profile.age %></td>
</tr>

<% profile.links.each do |link| %>
<tr>
<td><%= link_to "<b>#{link.name}</b> - #{link.url}".html_safe, href: link.url %></td>
<tr>
<% end %>
<tr>
<td><%= link_to 'Edit', edit_profile_path(profile) %></td>
<td><%= link_to 'Destroy', profile, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Profile', new_profile_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
resources :profiles do
resources :links
end

resources :linked_profiles, only: :index
end

0 comments on commit 8f1ec9b

Please sign in to comment.