我可以从主应用程序中软删除用户。 以下是详细信息。
用户网址为:
http://0.0.0.0:3000/users/4c7fa12c-3d58-480b-a823-6c67d6e7f0fe.json
这样会呈现JSON结果:
{ “ID”: “4c7fa12c-3d58-480b-a823-6c67d6e7f0fe”, “名称”: “约翰”, “状态”: “活跃”, “created_at”:“2015-11-10T18:31:27.000 00:00" , “的updated_at”: “2015-11-10T18:38:20.000 + 00:00”}
我可以通过这种方式使用失活进行软删除:
http://0.0.0.0:3000/users/4c7fa12c-3d58-480b-a823-6c67d6e7f0fe/inactivate
这是用户控制器中的我的inactivate方法:
def inactivate
@user.update status: 'Inactive'
redirect_to users_url
end
重新加载用户URL时显示结果:
{ “ID”: “4c7fa12c-3d58-480b-a823-6c67d6e7f0fe”, “名称”: “约翰”, “状态”: “无效”, “created_at”:“2015-11-10T18:31:27.000 00:00" , “的updated_at”: “2015-11-10T19:29:58.000 + 00:00”}
现在我的用户“处于非活动状态”。
这是我的路线档案:
Rails.application.routes.draw do
resources :wit_dims
resources :wokas
resources :posts
resources :languages
resources :users
root :to => "home#index"
resources :users do
get 'inactivate', on: :member
end
match 'inactivate', to: 'users#inactivate', via: 'delete'
#api
namespace :api do
namespace :v1 do
resources :users, only: [:index, :create, :show, :update, :destroy]
resources :posts, only: [:index, :create, :show, :update, :destroy]
end
end
end
如何更改此路由文件和API用户控制器以通过API实现同样的失效之王?我想要的不仅仅是一个简单的解决方案,使用PUT将用户的状态从Active更改为Inactive。
这是我的主要应用程序用户控制器:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy, :inactivate]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully inactivated.' }
format.json { head :no_content }
end
end
def inactivate
@user.update status: 'Inactive'
redirect_to users_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params[:user]
end
end
以下是我的API用户控制器:
class Api::V1::UsersController < Api::V1::BaseController
before_action :set_user, only: [:show, :update, :destroy]
def show
render(json: Api::V1::UserSerializer.new(@user).to_json)
end
def update
if @user.update_attributes(user_params)
render(
json: Api::V1::UserSerializer.new(@user).to_json,
status: 200,
location: api_v1_user_path(@user.id)
)
else
return api_error(status: 422, errors: @tbm.errors)
end
end
def index
users = User.all
render(
json: ActiveModel::ArraySerializer.new(
users,
each_serializer: Api::V1::UserSerializer,
root: 'users'
)
)
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :status).delete_if{ |k,v| v.nil? }
end
end
基本上我希望能够通过API调用一个inactivate方法,该方法将状态从Active更改为Inactive,而不是物理删除/销毁用户。
答案 0 :(得分:0)
我使用了PUT并对status属性执行了更新,将用户标记为Inactive。