How to update created_by field in scaffolding generated code?

筅森魡賤 提交于 2019-12-12 01:21:54

问题


I use devise gem for authentication. I generated a scaffold for model M. I would like to update the created_by field with the user id from the login page. How do I achieve this?

I have 2 fields in the model F1 and F2.

The form that scaffold creates shows input for users to enter values for F1 and F2. How do I update the value for created_by field using the current_user from devise? Because the create action seems to be entering only the fields from the form.

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

How to I update the model with current_user value in the above form without exposing that field to the user?

This is my controller:

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

回答1:


You need to add users reference to M model and add associations. created_by is not the best name for it. Let imagine that M is abbreviation for Music. In this case you need to create a migration

add_reference :musics, :user

Add to the Music model

belongs_to :user

And to the User model

has_many :musics

And change in the controller

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



回答2:


So simple in creating action change your method, for example, you just need to create the client

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

There should be a field like created_by exist in the table.



来源:https://stackoverflow.com/questions/54032436/how-to-update-created-by-field-in-scaffolding-generated-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!