diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 0000000..2357412 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -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 + 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 diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 0000000..2968277 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/app/models/article.rb b/app/models/article.rb index 8273373..85b3cd8 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/views/articles/_article.json.jbuilder b/app/views/articles/_article.json.jbuilder new file mode 100644 index 0000000..e04d27e --- /dev/null +++ b/app/views/articles/_article.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! article, :id, :created_at, :updated_at +json.url article_url(article, format: :json) \ No newline at end of file diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 0000000..4e0157d --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,36 @@ +<%= form_for article, :html => { :class => 'form-horizontal', multipart: true } do |f| %> + <% if article.errors.any? %> +
+

<%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:

+ + +
+ <% end %> +
+ <%= f.label :title %>
+ <%= f.text_field :title, autofocus: true %> +
+ +
+ <%= f.label :sort_description %>
+ <%= f.text_field :s_description, autofocus: true %> +
+ +
+ <%= f.label :long_description %>
+ <%= f.text_area :l_description, autofocus: true %> +
+ +
+ <%= f.label :article_images %>
+ <%= file_field_tag "images[]", type: :file,accept: 'image/png,image/gif,image/jpeg', multiple: true %> +
+
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 0000000..65565c0 --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Article

+ +<%= render 'form', article: @article %> + +<%= link_to 'Show', @article %> | +<%= link_to 'Back', articles_path %> diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 0000000..df0b73d --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,31 @@ +

Articles

+ + + + + + + + + + + + + + <% @articles.each do |article| %> + + + + + + + + + + <% end %> + +
Title Sort Description Long Description Author Action
<%= article.title %><%= article.s_description %><%= article.l_description %><%= article.user.name %><%= link_to 'Show', article %><%= link_to 'Edit', edit_article_path(article) %><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Article', new_article_path %> diff --git a/app/views/articles/index.json.jbuilder b/app/views/articles/index.json.jbuilder new file mode 100644 index 0000000..1f1649d --- /dev/null +++ b/app/views/articles/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @articles, partial: 'articles/article', as: :article \ No newline at end of file diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 0000000..056f863 --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,5 @@ +

New Article

+ +<%= render 'form', article: @article %> + +<%= link_to 'Back', articles_path %> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 0000000..717eab6 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,10 @@ + +

<%= @article.title %>

+

+

<%= @article.s_description %>

+

<%= @article.l_description %>

+

<%= @article.user.name %>

+

Published: <%= time_ago_in_words(@article.created_at) %> ago

+
+<%= link_to 'Edit', edit_article_path(@article) %> | +<%= link_to 'Back', articles_path %> diff --git a/app/views/articles/show.json.jbuilder b/app/views/articles/show.json.jbuilder new file mode 100644 index 0000000..b66797d --- /dev/null +++ b/app/views/articles/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "articles/article", article: @article \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 0653957..b40299c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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.' diff --git a/config/routes.rb b/config/routes.rb index a048c9e..319ca28 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20160921062609_update_column_to_article.rb b/db/migrate/20160921062609_update_column_to_article.rb new file mode 100644 index 0000000..034326c --- /dev/null +++ b/db/migrate/20160921062609_update_column_to_article.rb @@ -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