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

Finished CRUD for Article #5

Closed
wants to merge 1 commit into from
Closed
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
84 changes: 84 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, expect: [:index,:show]
# GET /articles
# GET /articles.json
def index
@articles = Article.all
end

# GET /articles/1
# GET /articles/1.json
def show
end

# GET /articles/new
def new
@article = Article.new
end

# GET /articles/1/edit
def edit
end

# POST /articles
# POST /articles.json
def create
@article = current_user.articles.build(article_params)
respond_to do |format|
if @article.save
if params[:images].present?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uzaif313 can you please extract this code to new private method i.e. save_image(image_param) ?

params[:images].each { |image|
@article.pictures.create(image: image)
}
end
format.html { redirect_to @article, notice: I18n.t('controller.create',model: 'Article') }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /articles/1
# PATCH/PUT /articles/1.json
def update
respond_to do |format|
if @article.update(article_params)
if params[:images].present?
@article.pictures.destroy_all
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uzaif313 same here..

params[:images].each { |image|
@article.pictures.create(image: image)
}
end
format.html { redirect_to @article, notice: I18n.t('controller.update',model: 'Article') }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end

# DELETE /articles/1
# DELETE /articles/1.json
def destroy
@article.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: I18n.t('controller.destroy',model: 'Article') }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:title,:s_description,:l_description,pictures_attributes: [:image])
end
end
2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ArticlesHelper
end
4 changes: 4 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class Article < ApplicationRecord
has_many :tags, :dependent => :destroy
has_many :pictures, as: :imageable
has_and_belongs_to_many :catgories
accepts_nested_attributes_for :pictures

validates :title, presence: true
validates :s_description, presence: true
end
2 changes: 2 additions & 0 deletions app/views/articles/_article.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! article, :id, :created_at, :updated_at
json.url article_url(article, format: :json)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need Space Here

Copy link
Owner

@charusat09 charusat09 Sep 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hardikupadhyay16 @uzaif313 I think we don't need this at all...

36 changes: 36 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%= form_for article, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
<% if article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2>

<ul>
<% article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title, autofocus: true %>
</div>

<div class="field">
<%= f.label :sort_description %><br />
<%= f.text_field :s_description, autofocus: true %>
</div>

<div class="field">
<%= f.label :long_description %><br />
<%= f.text_area :l_description, autofocus: true %>
</div>

<div class="field">
<%= f.label :article_images %><br />
<%= file_field_tag "images[]", type: :file,accept: 'image/png,image/gif,image/jpeg', multiple: true %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Article</h1>

<%= render 'form', article: @article %>

<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>
31 changes: 31 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1>Articles</h1>

<table>
<thead>
<tr>
<th> Title </th>
<th> Sort Description </th>
<th> Long Description </th>
<th> Author </th>
<th colspan="3"> Action </th>
</tr>
</thead>

<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.s_description %></td>
<td><%= article.l_description %></td>
<td><%= article.user.name %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Article', new_article_path %>
1 change: 1 addition & 0 deletions app/views/articles/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @articles, partial: 'articles/article', as: :article
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space here at the end of file

5 changes: 5 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Article</h1>

<%= render 'form', article: @article %>

<%= link_to 'Back', articles_path %>
10 changes: 10 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<h3><%= @article.title %></h3>
<hr><br>
<p><%= @article.s_description %></p>
<p><%= @article.l_description %></p>
<p><%= @article.user.name %></p>
<p>Published: <%= time_ago_in_words(@article.created_at) %> ago</p>
<br>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
1 change: 1 addition & 0 deletions app/views/articles/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "articles/article", article: @article
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space here at the end of file

5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
# available at http://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
controller:
create: '%{model} was successfully created.'
update: '%{model} was successfully updated.'
destroy: '%{model} was successfully destroyed.'
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
devise_for :users, controllers: {registrations: "users/registrations"}
get "*path" => redirect("/")
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :articles
end
11 changes: 11 additions & 0 deletions db/migrate/20160921062609_update_column_to_article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UpdateColumnToArticle < ActiveRecord::Migration[5.0]
def up
change_column :articles, :l_description, :text
remove_reference(:articles, :category, index: true, foreign_key: true)
end

def down
change_column :articles, :l_description, :string
add_reference(:articles, :category, index: true, foreign_key: true)
end
end