From ce280fffa13c7cb36bd5b6bca499c0cdd91a3937 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 28 Dec 2014 19:55:50 +0100 Subject: [PATCH] reporting things via ajax works now --- app/controllers/ajax/report_controller.rb | 32 ++++++++++++++++++++++ app/views/ajax/report/create.json.jbuilder | 2 ++ config/routes.rb | 1 + 3 files changed, 35 insertions(+) create mode 100644 app/controllers/ajax/report_controller.rb create mode 100644 app/views/ajax/report/create.json.jbuilder diff --git a/app/controllers/ajax/report_controller.rb b/app/controllers/ajax/report_controller.rb new file mode 100644 index 00000000..ee03a461 --- /dev/null +++ b/app/controllers/ajax/report_controller.rb @@ -0,0 +1,32 @@ +class Ajax::ReportController < ApplicationController + def create + params.require :id + params.require :type + + @status = :err + @success = false + + if current_user.nil? + @message = "login required" + return + end + + unless %w(answer comment question user).include? params[:type] + @message = "unknown type" + return + end + + object = params[:type].strip.capitalize.constantize.find params[:id] + + if object.nil? + @message = "Could not find #{params[:type]}" + return + end + + current_user.report object + + @status = :okay + @message = "#{params[:type].capitalize} reported. A moderator will decide what happens with the #{params[:type]}." + @success = true + end +end diff --git a/app/views/ajax/report/create.json.jbuilder b/app/views/ajax/report/create.json.jbuilder new file mode 100644 index 00000000..f2c8c8c4 --- /dev/null +++ b/app/views/ajax/report/create.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! 'ajax/shared/status' +json.render @render \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index cf93af44..c1481105 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,6 +62,7 @@ Rails.application.routes.draw do match '/create_smile', to: 'smile#create', via: :post, as: :create_smile match '/destroy_smile', to: 'smile#destroy', via: :post, as: :destroy_smile match '/create_comment', to: 'comment#create', via: :post, as: :create_comment + match '/report', to: 'report#create', via: :post, as: :report end match '/public', to: 'public#index', via: :get, as: :public_timeline