Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Add edit , update , show and inex listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Abdelsalam committed Jul 11, 2016
1 parent e6d5872 commit 1fe48e9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
18 changes: 18 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
class ArticlesController < ApplicationController
def index
@articles = Article.all
end

def new
@article = Article.new
end

def edit
@article = Article.find(params[:id])
end

def update
@article = Article.find(params[:id])
if @article.update(article_params)
flash[:notice] = "Article was successfully updated"
redirect_to article_path(@article)
else
render 'edit'
end
end

def create
@article = Article.new(article_params)
if @article.save
Expand Down
25 changes: 25 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Edit existing article</h2>
<% if @article.errors.any? %>
<h3>The following errors prevented the article from getting edited</h3>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>

<%= form_for @article do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title%>
</p>

<p>
<%= f.label :description %></br>
<%= f.text_area :description %>
</p>
<%= f.submit %>


<% end %>
<%= link_to 'Back to articles' , articles_path %>
19 changes: 19 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1> All Articles </h1>
<%= link_to "Create new article" , new_article_path %>

<style>table{border:1px solid #ccc;padding:15px;}tr{}</style>
<table border="1">
<tr>
<th>Title</th>
<th>Description</th>
</tr>

<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show', article_path(article) %></td>
</tr>
<% end %>
</table>
14 changes: 8 additions & 6 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
<%= f.text_field :title%>
</p>

<p>
<%= f.label :description %></br>
<%= f.text_area :description %>
</p>
<%= f.submit %>
<p>
<%= f.label :description %></br>
<%= f.text_area :description %>
</p>
<%= f.submit %>


<% end %>
<% end %>

<%= link_to 'Back to articles' , articles_path %>
4 changes: 3 additions & 1 deletion app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
Title: <%= @article.title %>
</p>

<p>Description:<%= @article.description %></p>
<p>Description:<%= @article.description %></p>
<%= link_to 'Edit' , edit_article_path(@article) %> |
<%= link_to 'Back to articles' , articles_path %>

0 comments on commit 1fe48e9

Please sign in to comment.