Rails -表单提交时没有创建新字段



所以我有以下模型。用户、团队& &;具有以下定义的成员

用户

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
belongs_to :current_team, class_name: "Team", optional: true
has_many :memberships
has_many :teams, through: :memberships
has_many :owner_memberships, -> { where(role: :owner) }, class_name: "Membership"
has_many :owned_teams, through: :owner_memberships, source: :team
def membership_for(team)
memberships.where(team: team).take
end
end

团队
class Team < ApplicationRecord
has_many :memberships
has_many :users, through: :memberships
has_one :owner_membership, -> { where(role: :owner, status: :active) }, class_name: "Membership"
has_one :owner, through: :owner_membership, source: :user
end

加入

class Membership < ApplicationRecord
belongs_to :user
belongs_to :team
end

使用scaffold生成的所有表单都可以正常工作。我现在面临的问题是,我已经为会员模型添加了2个额外的字段,Role&Status意味着成员类现在看起来像下面,但我正在努力将这些字段传递给数据库。

class Membership < ApplicationRecord
belongs_to :user
belongs_to :team
enum status: [:pending, :active]
enum role: [:owner, :admin, :user]

def owner_or_admin?
owner? || admin?
end
end

当我查看rails s的控制台时,我可以看到以下内容,这表明新字段没有作为成员定义的一部分发送,而是作为单独的参数发送。如何让它们正确地发送和创建

Processing by MembershipsController#create as HTML
Parameters: {
"authenticity_token"=>"5gzC/7YLIzqb+uNqHi2izth07MPv4WXrF49444+2bK7ML7ceLwk+BR2tP9fHiqCVJFJrxJHjuaz7dXxcp0yq0A==", 
"membership"=>{"user_id"=>"2", "team_id"=>"1"}, 
"role"=>"0", 
"status"=>"0", 
"commit"=>"Create Membership"
}

Membership Create (1.3ms)  INSERT INTO "memberships" ("user_id", "team_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["user_id", 2], ["team_id", 1], ["created_at", "2021-08-30 14:58:03.929323"]
编辑:为表单添加视图
<%= form_with(model: membership, local: true) do |form| %>
<% if membership.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(membership.errors.count, "error") %> prohibited this membership from being saved:</h2>
<ul>
<% membership.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :user_id %>
<%= form.collection_select :user_id, User.order(:email),:id,:email, include_blank: true %>
</div>
<div class="field">
<%= form.label :team_id %>
<%= form.collection_select :team_id, Team.where(id: current_team.id),:id,:name, :selected => current_team %>
</div>
<div class="field">
<%= form.label :role %>    
<%= select_tag :role, options_for_select(Membership.roles.map {|k, v| [k.humanize.capitalize, v]}) %>
</div>
<div class="field">
<%= form.label :status %>    
<%= select_tag :status, options_for_select(Membership.statuses.map {|k, v| [k.humanize.capitalize, v]}) %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

首先设置一个方法,将枚举状态作为适合构建选择器、复选框或单选按钮的[label, value]对数组:

module EnumHelper
# creates an array of [label, value] pairs from an enum
# the translations can be customized through for example:
# -  membership.statuses.active
# -  membership.roles.active
def options_from_enum(klass, name)
i18n_scope = "#{klass.model_name.i18n_key}.#{name}"
klass.public_send(name).map do |key|
label = I18n.t(
key,
default: key.to_s.humanize
scope: i18n_scope 
)
[label, key]
end
end
end

请注意,从表单传递的值应该是枚举状态的名称("pending"),而不是整数("0"),这是一个内部实现细节,应该只有模型知道。

然后确保在表单构建器之外创建输入:

<%= form_with(model: @membership) do |form| %>
<div class="field">
<%= form.label :role %>
<%= form.select :role, options_from_enum(Membership, :roles) %>
</div>
<div class="field">
<%= form.label :status %>
<%= form.select :status, options_from_enum(Membership, :statuses) %>
</div>

# ...
<% end %>

相关内容

  • 没有找到相关文章

最新更新