无法访问控制器中的 current_user 以保存关系
Unable to access current_user in controller to save relation
我有一个 Rails 6 项目,我 Citusdata/activerecord-multi-tenant gem 来处理基于行的多租户 我正在尝试为用户创建一个时间表
因为现在我无法访问 current_user 来保存关系。我的控制台输出根本没有帮助:
它正在将关系连接到帐户模型并自动保存我的 account_id,但无法收集 user.id 或 current_user.id
我可以在新块和创建块中放置一个 byebug,但是当我在下面传递 byebug 时 @time_sheet.save
它返回为 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
我的新时光sheet表格
<%= 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" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:10:in `set_account_as_tenant'
Account Load (0.3ms) SELECT "accounts".* FROM "accounts" LIMIT [["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 时没有正确传递 user_id。
通过将 current_user id 合并到您的 time_sheet_params 中来更改您的新方法以包含它,如下所示:
@time_sheet = TimeSheet.new(time_sheet_params.merge(user_id: current_user.id)
我有一个 Rails 6 项目,我 Citusdata/activerecord-multi-tenant gem 来处理基于行的多租户 我正在尝试为用户创建一个时间表
因为现在我无法访问 current_user 来保存关系。我的控制台输出根本没有帮助:
它正在将关系连接到帐户模型并自动保存我的 account_id,但无法收集 user.id 或 current_user.id
我可以在新块和创建块中放置一个 byebug,但是当我在下面传递 byebug 时 @time_sheet.save
它返回为 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
我的新时光sheet表格
<%= 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" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:10:in `set_account_as_tenant'
Account Load (0.3ms) SELECT "accounts".* FROM "accounts" LIMIT [["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 时没有正确传递 user_id。 通过将 current_user id 合并到您的 time_sheet_params 中来更改您的新方法以包含它,如下所示:
@time_sheet = TimeSheet.new(time_sheet_params.merge(user_id: current_user.id)