【问题标题】:Unable to access current_user in controller to save relation无法访问控制器中的 current_user 以保存关系
【发布时间】:2020-03-26 09:39:11
【问题描述】:

我有一个 Rails 6 项目,我是 Citusdata/activerecord-multi-tenant gem 来处理基于行的 MultiTenancy 我正在尝试为用户创建一个 TimeSheet

因为现在我无法访问 current_user 来保存关系。我的控制台输出根本没有帮助:

它正在将关系连接到帐户模型并自动保存我的 account_id,但无法收集 user.id 或 current_user.id

我可以在 new 和 create 块中删除一个 byebug,但是当我通过 @time_sheet.save 下面的 byebug 时,它会返回为 nil。

我卡在这里,不知道我哪里出错了?我过去做过很多这样的事情。

这是所有设置的方式:

class User < ApplicationRecord
  extend FriendlyId
  friendly_id :username, use: :slugged

  multi_tenant :account
  belongs_to :role

  has_many :time_sheets
end

class TimeSheet < ApplicationRecord
  multi_tenant :account
  belongs_to :user (user_id in schema)
end

class TimeSheetsController < ApplicationController
before_action :authenticate_user!

  def new
    @user = current_user
    @time_sheet = TimeSheet.new
  end

  def create
    @user = current_user
    @time_sheet = TimeSheet.new(time_sheet_params)

    respond_to do |format|
      if @time_sheet.save
        format.html { redirect_to @time_sheet, flash: { success: "#{@time_sheet.start_date} - #{@time_sheet.end_date} has been sucessfully created." } }
      else
        format.html { render :new }
      end
    end
  end

end

我的新时间表表格

<%= form_for(@time_sheet, :html => { class: "flex w-full items-center mt-64" }) do |f| %>
<% if @time_sheet.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@time_sheet.errors.count, "error") %> prohibited this user from being saved:</h2>

    <ul>
      <% @time_sheet.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
<% end %>
  <div class="w-full text-center">
    <p class="text-3xl text-primary-700 mb-16">New Time Sheet</p>
    <div class="flex flex-wrap w-full justify-center">
      <div class="w-1/4 mr-3">
        <%= f.label :start_date, "Start Date", class: 'form-label text-left' %>
        <%= f.date_field :start_date, class: 'form-input w-full' %>
      </div>
      <div class="w-1/4 ml-3">
        <%= f.label :end_date, "End Date", class: 'form-label text-left' %>
        <%= f.date_field :end_date, class: 'form-input w-full' %>
      </div>
    </div>
    <div class="flex w-full justify-center">
      <div class="w-1/4 mt-6">
        <%= f.submit "Start Period", class: "p-3 w-full bg-green-800" %>
      </div>
    </div>
  </div>
<% end %>

我的控制台输出:

Started POST "/time_sheets" for 127.0.0.1 at 2020-03-26 09:43:34 -0600
Processing by TimeSheetsController#create as HTML
  Parameters: {"authenticity_token"=>"TuSzdfVr5iYqpvHfoPU+W7dFKQHCSX9cYcvbtqiqwdKOmaxhqQVp0inOjI0/NYuMpBoZPzJggJdeVro9XyV/lQ==", "time_sheet"=>{"start_date"=>"2020-03-26", "end_date"=>"2020-03-27"}, "commit"=>"Start Period"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:10:in `set_account_as_tenant'
  Account Load (0.3ms)  SELECT "accounts".* FROM "accounts" LIMIT $1  [["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:12:in `set_account_as_tenant'
Completed 422 Unprocessable Entity in 10ms (ActiveRecord: 1.1ms | Allocations: 3992)



ActiveRecord::RecordInvalid (Validation failed: User must exist):

app/controllers/time_sheets_controller.rb:21:in `block in create'
app/controllers/time_sheets_controller.rb:20:in `create'

【问题讨论】:

  • 控制台输出是什么?如果您将控制器创建操作从 @time_sheet.save 更改为 @time_sheet.save!,终端输出什么?它应该解释为什么验证失败
  • @Mark 请参阅上面的编辑以获取控制台输出。

标签: ruby-on-rails devise ruby-on-rails-6


【解决方案1】:

看起来您在创建 time_sheet 时没有正确传递 user_id。 更改您的新方法以包含 current_user id,方法是将其合并到您的 time_sheet_params 中,如下所示:

@time_sheet = TimeSheet.new(time_sheet_params.merge(user_id: current_user.id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2018-08-05
    • 2016-06-12
    相关资源
    最近更新 更多