[英]Devise - How to display user that updated others post
I would like users to be able to create/update my "Person" resource, including overwriting each other. 我希望用户能够创建/更新“人”资源,包括相互覆盖。 Currently I'm able to capture the user who created the initial "Person" but i can't figure out how to capture and display the user that updated the resource. 目前,我可以捕获创建初始“人员”的用户,但是我不知道如何捕获和显示更新资源的用户。
For example if user 1 creates an item, then user 2 updates this item, I would like to display that this item was most recently edited by user 2 . 例如,如果用户1创建了一个项目,然后用户2更新了该项目,我想显示该项目是用户2最近编辑的。
Here's my controller, any help would be much appreciated thanks! 这是我的控制器,非常感谢您的帮助!
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /people
# GET /people.json
def index
@people = Person.all
end
# GET /people/1
# GET /people/1.json
def show
end
# GET /people/new
def new
@person = current_user.person.build
end
# GET /people/1/edit
def edit
end
# POST /people
# POST /people.json
def create
@person = current_user.person.build(person_params)
respond_to do |format|
if @person.save
format.html { redirect_to @person, notice: 'Person was successfully created.' }
format.json { render action: 'show', status: :created, location: @person }
else
format.html { render action: 'new' }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /people/1
# PATCH/PUT /people/1.json
def update
respond_to do |format|
if @person.update(person_params)
format.html { redirect_to @person, notice: 'Person was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
# DELETE /people/1
# DELETE /people/1.json
def destroy
@person.destroy
respond_to do |format|
format.html { redirect_to people_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_person
@person = Person.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def person_params
params.require(:person).permit(:name, :twitter, :facebook, :instagram, :vine)
end
end
在posts
表中创建updated_by
列,每当用户更新帖子时,都使用current_user
的值更新updated_by
列。
Simple way for doing it is to maintain the a column called updated_by and store the current user when its updated as @Andrey mentioned in previous comment. 一种简单的方法是维护一个名为updated_by的列,并在将其更新为先前评论中提到的@Andrey时存储当前用户。
But if your looking for a more extensive for tracking you can use auditable gem 但是,如果您希望更广泛地进行跟踪,则可以使用可审核的 gem
You can check this out : https://github.com/harley/auditable 您可以查看以下内容: https : //github.com/harley/auditable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.