如何更新脚手架生成代码中的 created_by 字段?

How to update created_by field in scaffolding generated code?

我使用 devise gem 进行身份验证。我为模型 M 生成了一个脚手架。我想用登录页面的用户 ID 更新 created_by 字段。我该如何实现?

我在模型中有 2 个字段 F1F2

脚手架创建的表单显示供用户输入 F1F2 值的输入。如何使用 devise 中的 current_user 更新 created_by 字段的值?因为创建操作似乎只输入表单中的字段。

<%= form_with(model: M, local: true) do |form| %>
  <% if M.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(M.errors.count, "error") %> prohibited this movie from being saved:</h2>

      <ul>
      <% M.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :F1 %>
    <%= form.text_field :F1 %>
  </div>

  <div class="field">
    <%= form.label :F2 %>
    <%= form.text_field :F2 %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

如何在不向用户公开该字段的情况下使用上述表单中的 current_user 值更新模型?

这是我的控制器:

class MsController < ApplicationController
before_action :set_M, only: [:show, :edit, :update, :destroy]

# GET /Ms
# GET /Ms.json
def index
@Ms = M.all

@categories = @Ms.uniq.pluck(:category)
@Ms_by_category = Hash.new

@categories.each do |category|
  @Ms_by_category[category] = M.where(:category => category)
end     
end

# GET /Ms/1
# GET /Ms/1.json
def show
end

# GET /Ms/new
def new
@M = M.new
end

# GET /Ms/1/edit
def edit
end

# POST /Ms
# POST /Ms.json
def create
@M = M.new(M_params)

respond_to do |format|
  if @M.save
    format.html { redirect_to @M, notice: 'M was successfully created.' }
    format.json { render :show, status: :created, location: @M }
  else
    format.html { render :new }
    format.json { render json: @M.errors, status: :unprocessable_entity }
  end
end
end

# PATCH/PUT /Ms/1
# PATCH/PUT /Ms/1.json
def update
respond_to do |format|
  if @M.update(M_params)
    format.html { redirect_to @M, notice: 'M was successfully updated.' }
    format.json { render :show, status: :ok, location: @M }
  else
    format.html { render :edit }
    format.json { render json: @M.errors, status: :unprocessable_entity }
  end
end
end

# DELETE /Ms/1
# DELETE /Ms/1.json
def destroy
@M.destroy
respond_to do |format|
  format.html { redirect_to Ms_url, notice: 'M was successfully destroyed.' }
  format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_M
  @M = M.find(params[:id])     
end

# Never trust parameters from the scary internet, only allow the white list through.
def M_params
  params.require(:M).permit(:title, :category, :rating)
end
end

创建action如此简单,改变你的方法,例如,你只需要创建客户端

before_action :authenticate_user!

def create
  @client = Client.new(name: params[:name], address: params[:address],created_by: current_user.email  )
  if @client.save
    redirect_to @client
  else 
    render 'new'
  end
end

table 中应该有像 created_by 这样的字段。

您需要添加用户对 M 模型的引用并添加关联。 created_by 不是最好的名字。假设 M 是 Music 的缩写。在这种情况下,您需要创建一个迁移

add_reference :musics, :user

添加到音乐模型

belongs_to :user

以及用户模型

has_many :musics

并更改控制器

def new
  @music = current_user.musics.new
end

def create
  @music = current_user.musics.new(M_params)

  respond_to do |format|
    if @music.save
      format.html { redirect_to @music, notice: 'Music was successfully created.' }
      format.json { render :show, status: :created, location: @music }
    else
      format.html { render :new }
      format.json { render json: @music.errors, status: :unprocessable_entity }
    end
  end
end