【问题标题】:Devise - How to display user that updated others post设计 - 如何显示更新其他帖子的用户
【发布时间】:2015-03-24 06:35:56
【问题描述】:

我希望用户能够创建/更新我的“Person”资源,包括相互覆盖。目前我能够捕获创建初始“人”的用户,但我无法弄清楚如何捕获和显示更新资源的用户。

例如,如果 user 1 创建了一个项目,然后 user 2 更新了这个项目,我想显示这个项目是 user 最近编辑的2

这是我的控制器,非常感谢您的帮助!

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise controller


    【解决方案1】:

    这样做的简单方法是维护一个名为 updated_by 的列,并在其更新时存储当前用户,如先前评论中提到的@Andrey。
    但是,如果您正在寻找更广泛的跟踪方式,您可以使用 auditable gem
    你可以看看这个: https://github.com/harley/auditable

    【讨论】:

      【解决方案2】:

      posts表中创建updated_by列,每次用户更新帖子时,将updated_by列更新为current_user的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-14
        • 1970-01-01
        • 2020-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多