Skip to content

Commit

Permalink
Add logic for edit articles
Browse files Browse the repository at this point in the history
  • Loading branch information
elpotatoslayer committed Apr 12, 2021
1 parent 6c7e20e commit 318e222
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ def create
end
end

def edit
set_article
@categories = Category.where.not(id: @article.categories)
end

def update
set_article
@categories = Category.where(id: article_category_params[:category_id])
if @article.update(article_params)
@categories.each { |cat| @article.categories << cat } unless @categories.empty?
redirect_to categories_path, notice: 'You successfully edited your article'
else
@article.valid?
flash.now.alert = "Sorry! #{@article.errors.full_messages.first}!"
@categories = Category.where.not(id: @article.categories)
render :edit
end
end

private

def authenticate_user
Expand Down

0 comments on commit 318e222

Please sign in to comment.