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

时间:2019-01-04 02:29:49

标签: ruby-on-rails scaffolding

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

我在模型F1F2中有2个字段。

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

<%= 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

2 个答案:

答案 0 :(得分:2)

创建动作非常简单,可以更改您的方法,例如,您只需要创建客户端

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

表中应该存在诸如created_by之类的字段。

答案 1 :(得分:1)

您需要添加对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