Я использую devise
gem для аутентификации.Я сгенерировал леску для модели M
.Я хотел бы обновить поле created_by
с помощью идентификатора пользователя со страницы входа в систему.Как мне этого добиться?
У меня есть 2 поля в модели F1
и F2
.
Форма, которую создает скаффолд, показывает ввод данных для пользователей для ввода значений для F1
иF2
.Как обновить значение для поля created_by
, используя current_user
из devise
?Поскольку действие create, по-видимому, вводит только поля из формы.
<%= 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