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? %> +
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?' } %> | +
<%= @article.s_description %>
+<%= @article.l_description %>
+<%= @article.user.name %>
+Published: <%= time_ago_in_words(@article.created_at) %> ago
+