-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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? | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ArticlesHelper | ||
end |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need Space Here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hardikupadhyay16 @uzaif313 I think we don't need this at all... |
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 %> |
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 %> |
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 %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @articles, partial: 'articles/article', as: :article | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space here at the end of file |
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 %> |
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 %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "articles/article", article: @article | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space here at the end of file |
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 |
There was a problem hiding this comment.
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) ?