Skip to content

Commit

Permalink
link CRUD completed
Browse files Browse the repository at this point in the history
  • Loading branch information
kakas committed Sep 6, 2015
1 parent aad5248 commit 4842543
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
28 changes: 27 additions & 1 deletion app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,51 @@ class LinksController < ApplicationController


def index
@link = Link.new
@links = Link.all
end

def show
@link = Link.find(params[:id])
end

def new
@link = Link.new
end

def create
@link = Link.new(link_params)

if @link.save
redirect_to root_path, notice: "Adds link successful"
else
render :new
end
end

def edit
@link = Link.find(params[:id])
end

def update
@link = Link.find(params[:id])

if @link.update(link_params)
redirect_to root_path, notice: "Edits link successful"
else
render :edit
end
end

def destroy
@link = Link.find(params[:id])
@link.destroy
redirect_to root_path, alert: "link has been deleted"
end

private

def link_params
params.require(:link).permit(:title, :url)
end

end
20 changes: 20 additions & 0 deletions app/views/layouts/_navbar.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%= link_to "RADDIT", root_path, class: "navbar-brand" %>
</div>

<div id="nav-collapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Sign up", "#" %></li>
<li><%= link_to "Sign in", "#" %></li>
</ul>
</div>
</div>
</nav>
9 changes: 8 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
</head>
<body>

<%= yield %>
<div class="container">
<%= render "layouts/navbar" %>

<div class="col-md-8 col-md-offset-2">
<%= yield %>
</div>
</div>


</body>
</html>
6 changes: 6 additions & 0 deletions app/views/links/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Edit link</h1>
<%= simple_form_for @link do |f| %>
<%= f.input :title %>
<%= f.input :url %>
<%= f.button :submit, "Submmit", class: "btn btn-primary btn-lg" %>
<% end %>
20 changes: 19 additions & 1 deletion app/views/links/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
<h1>Index</h1>

<% @links.each do |link| %>
<h2><%= link_to link.title, link_path(link) %></h2>
<h4>
<small>
Submitted
<%= time_ago_in_words(link.created_at) %>
by xxx
</small>
</h4>
<div class="btn-group" role="group">
<button class="btn btn-default" type="button">
<%= link_to "Visit link", link.url %>
</button>
<button class="btn btn-default" type="button">Upvote</button>
<button class="btn btn-default" type="button">Downvote</button>
</div>
<hr>
<% end %>
7 changes: 7 additions & 0 deletions app/views/links/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<h1>Submit New Link</h1>
<%= simple_form_for @link do |f| %>
<%= f.input :title %>
<%= f.input :url %>
<%= f.button :submit, "Submmit", class: "btn btn-primary btn-lg" %>
<% end %>
29 changes: 29 additions & 0 deletions app/views/links/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

<h2><%= link_to @link.title, @link.url %></h2>
<h4>
<small>
Submitted
<%= time_ago_in_words(@link.created_at) %>
by xxx
</small>
</h4>

<hr>

<div class="btn-group" role="group">
<button class="btn btn-default" type="button">Visit Link</button>
</div>

<div class="btn-group" role="group">
<button class="btn btn-default" type="button">
<%= link_to "Edit", edit_link_path %>
</button>
<button class="btn btn-default" type="button">
<%= link_to "Destroy", link_path(@link), method: :delete, data: {confirm: "Are you sure?"} %>
</button>
</div>

<div class="btn-group pull-right" role="group">
<button class="btn btn-default" type="button">Upvote</button>
<button class="btn btn-default" type="button">Downvote</button>
</div>

0 comments on commit 4842543

Please sign in to comment.