Allow changing user for comments in admin panel (#113)

* Allow changing user for comments in admin panel

* Add successful flash msg

* Use user_id instead of name for comment admin
This commit is contained in:
Andy Zhao 2018-03-20 16:25:09 -04:00 committed by Mac Siri
parent 10ba6ef14d
commit f5913399aa
3 changed files with 17 additions and 13 deletions

View file

@ -1,19 +1,21 @@
module Admin
class CommentsController < Admin::ApplicationController
# To customize the behavior of this controller,
# simply overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Comment.all.paginate(10, params[:page])
# end
def update
comment = Comment.find(params[:id])
if comment.update(comment_params)
flash[:notice] = "Comment successfully updated"
redirect_to "/admin/comments/#{comment.id}"
else
flash.now[:error] = comment.errors.full_messages
render :new, locals: { page: Administrate::Page::Form.new(dashboard, comment) }
end
end
# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Comment.find_by!(slug: param)
# end
private
# See https://administrate-docs.herokuapp.com/customizing_controller_actions
# for more information
def comment_params
accessible = %i[user_id body_markdown deleted]
params.require(:comment).permit(accessible)
end
end
end

View file

@ -4,6 +4,7 @@ module Admin
user = User.find(params[:id])
UserRoleService.new(user).check_for_roles(params[:user])
if user.errors.messages.blank? && user.update(user_params)
flash[:notice] = "User successfully updated"
redirect_to "/admin/users/#{params[:id]}"
else
render_with_errors(user)

View file

@ -61,6 +61,7 @@ class CommentDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [
:user_id,
:body_markdown,
:deleted,
]