活动模型::帐户中缺少属性错误::撤退控制器#创建



我浏览了StackOverflow,但在这里找不到适合我的用例的解决方案。

我正在尝试在创建撤退时将一个或多个团队附加到撤退。

我有一个多租户应用程序,该应用程序具有邀请用户的帐户所有者。然后,可以将用户添加到团队。

如果在表单中没有选择团队,我可以成功创建闭关,但是,当选择一个团队并在选择该团队的情况下创建撤退时,我收到以下错误:

ActiveModel::MissingAttributeError in Accounts::RetreatsController#create
can't write unknown attribute `retreat_id`
Parameters:
{"authenticity_token"=>"AcmWMmv+rhwzLKsxi9RykkhhquaZuTBqSBj87acOdDOo+uXycQOWHoeuNzDEGTQw+o7ewwS4EB8uTyIy7x5vsA==",
"retreat"=>{"name"=>"bbb", "description"=>"dsfsdf", "team_ids"=>["56"]},
"commit"=>"Create Retreat"}

这是我的模型:

团队.rb

class Team < ApplicationRecord
belongs_to :account
has_many_and_belongs_to :retreat
has_many :team_members
has_many :users, through: :team_members
accepts_nested_attributes_for :team_members
end

撤退.rb

class Retreat < ApplicationRecord
belongs_to :user
belongs_to :account
validates :name, presence: true, uniqueness: true
has_many :teams
accepts_nested_attributes_for :teams
end

这是控制器:

module Accounts
class RetreatsController < Accounts::BaseController
before_action :authenticate_user!
before_action :set_retreat, only: [:show, :edit, :update, :destroy]
def index
@retreats = current_account.retreats.all.order("created_at DESC")
end
def show
end
def new
@retreat = current_account.retreats.build
@team = current_account.teams.all
end
def edit
end
def create
@retreat = current_account.retreats.build(retreat_params)
@retreat.user_id = current_user.id
respond_to do |format|
if @retreat.save
format.html { redirect_to @retreat, notice: 'Retreat was successfully created.' }
format.json { render :show, status: :created, location: @retreat }
else
format.html { render :new }
format.json { render json: @retreat.errors, status: :unprocessable_entity }
end
end
end
def destroy
@retreat.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Retreat was successfully deleted.' }
format.json { head :no_content }
end
end
private
def set_retreat
@retreat = current_account.retreats.find(params[:id])
end
def retreat_params
params.require(:retreat).permit(:name, :description, team_ids:[])
end
end
end

这是视图:

<div class="w-full lg:w-12/12 bg-white p-5 rounded-lg lg:rounded-l-none">
<%= simple_form_for(@retreat) do |f| %>
<%= f.error_notification %>
<form class="px-8 pt-6 pb-8 mb-4 bg-white rounded">
<div class="mb-4">
<div class="w-full mb-4 md:mr-2 md:mb-0">
<label class="block mb-2 text-sm font-bold text-gray-700" for="firstName">
Name your retreat
</label>
<%= f.input :name, autofocus: true, class: 'w-full px-3 py-2 mb-3 text-sm leading-tight text-gray-700 border rounded shadow appearance-none focus:outline-none focus:shadow-outline', label: false %>
</div>
</div>
<div class="mb-4">
<div class="w-full mb-4 md:mr-2 md:mb-0">
<label class="block mb-2 text-sm font-bold text-gray-700" for="firstName">
Give it a description
</label>
<%= f.text_area :description, autofocus: true, class: 'no-resize appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500 h-48 resize-none', label: false %>
</div>
</div>
<div class="field">
<div class="control">
<%= f.label "Add team(s) to your retreat", class:"label" %><br />
<div class="control has-icons-left">
<%= f.collection_check_boxes :team_ids, current_account.teams.all, :id, :name, include_hidden: false do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
<% end %>
</div>
</div>
</div>
<div class="field">
<div class="control">
<%= f.button :submit, class: "w-full px-4 py-2 font-bold text-white rounded-full bg-green-500 hover:bg-green-700 focus:outline-none focus:shadow-outline" %>
</div>
</div>
<%= link_to 'Back', retreats_path, class:'button' %>
</form>
<% end %>
</div>

这是我的架构:

create_table "retreats", force: :cascade do |t|
t.string "name"
t.text "description"
t.bigint "user_id", null: false
t.bigint "account_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "team_id"
t.index ["account_id"], name: "index_retreats_on_account_id"
t.index ["user_id"], name: "index_retreats_on_user_id"
end
create_table "teams", force: :cascade do |t|
t.string "name"
t.bigint "account_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_teams_on_account_id"
end
create_table "team_members", force: :cascade do |t|
t.bigint "team_id", null: false
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["team_id"], name: "index_team_members_on_team_id"
t.index ["user_id"], name: "index_team_members_on_user_id"
end

我最终在闭关和闭关团队之间添加了一个连接表,以及引用闭关和团队的连接表。

接受的评论建议使用 has_and_belongs_to_many 关联,但我相信这对于当前的 rails 版本来说是折旧的。因此使用了接受嵌套属性的直通。

您希望在RetreatTeam之间建立多对多关联。为此,您需要创建一个连接表retreats_teams,其中包含列retreat_idteam_id,然后

Retreat模型中:

has_and_belongs_to_many :teams

Team模型中:

has_and_belongs_to_many :retreats

有关多对多关联的更多信息,请参阅"在has_many :throughhas_and_belongs_to_many之间进行选择"。

最新更新