API de comptabilité horaire.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

activities_controller.rb 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class Api::V1::ActivitiesController < ApplicationController
  2. before_action :set_activity, only: %i[show update destroy]
  3. before_action :check_login
  4. def index
  5. render json: ActivitySerializer.new(Activity.all).serializable_hash.to_json
  6. end
  7. def show
  8. render json: ActivitySerializer.new(@activity).serializable_hash.to_json
  9. end
  10. def create
  11. activity = current_user.created_activities.build(activity_params)
  12. if activity.save
  13. JoinedUserActivity.create!(
  14. user_id: current_user.id,
  15. activity_id: activity.id
  16. )
  17. render json: ActivitySerializer.new(activity).serializable_hash.to_json, status: :created
  18. else
  19. render json: { errors: activity.errors }, status: :unprocessable_entity
  20. end
  21. end
  22. def update
  23. if @activity.update(activity_params)
  24. render json: ActivitySerializer.new(@activity).serializable_hash.to_json, status: :ok
  25. else
  26. render json: @activity.errors, status: :unprocessable_entity
  27. end
  28. end
  29. def destroy
  30. @activity.destroy
  31. head 204
  32. end
  33. private
  34. # Only allow a trusted parameter "white list" through.
  35. def activity_params
  36. params.require(:activity).permit(:name, :author_id, :description, :client)
  37. end
  38. def set_activity
  39. @activity = Activity.find(params[:id])
  40. end
  41. end