仅针对 3 个 Rails 表单 (collection_select) 元素中的 2 个出现提示

Prompt appears for only 2 of 3 Rails form (collection_select) elements

我是 Rails 初学者。 我有一个包含多个 collection_select 元素(类别、银行帐户、供应商)的表单。 Category 和 Bank Account 字段正确显示提供的提示,但 Vendor 字段不显示其提示。而是显示第一个供应商,并且提示根本不会出现在该下拉列表中。

我很困惑,因为银行账户字段非常相似,但它工作正常。

这是一张照片:

app/views/register_transactions/new.html.erb

<div>
   <h1>New Register Transaction</h1>
   <%= render 'form', register_transaction: @register_transaction %>
</div>

app/views/register_transactions/_form.html.erb

<%= form_with(model: @register_transaction) do |f| %>

  <%= f.label :date %>
  <%= f.date_field :date %>

  <%= f.label :category_id %>
  <%= f.grouped_collection_select(:category_id, 
                                  Category.top_level, 
                                  :subcategories, 
                                  :name, 
                                  :id, 
                                  :name,
                                  { prompt: 'Select a category'}) %>

  <%= f.label :vendor_id %>
  <%= f.collection_select :vendor_id, 
                          Vendor.order(:name), 
                          :id, 
                          :name, 
                          {prompt: 'Select a vendor'} %>

  <%= f.label :amount %>
  <%= f.text_field :amount %>

  <%= f.label :bank_account_id%>
  <%= f.collection_select :bank_account_id, 
                          BankAccount.order(:name), 
                          :id, 
                          :name, 
                          {prompt: 'Select an account'} %>

  <%= f.label :memo %>
  <%= f.text_field :memo %>

  <%= f.submit %>
<% end %>

app/controllers/register_transactions_controller.rb

class RegisterTransactionsController < ApplicationController

  ... abbrieviated...

  def new
    @register_transaction = RegisterTransaction.new
  end
  
  private
    def register_transaction_params
      params.require(:register_transaction).permit(:date, :category_id, :vendor_id, :amount, :bank_account_id, :memo)
    end
end

app/models/vendor.rb

class Vendor < ApplicationRecord
  validates :name, uniqueness: true, presence: true
  has_many :register_transactions, dependent: :nullify
end

app/models/bank_account.rb

class BankAccount < ApplicationRecord
  ACCOUNT_TYPES = %w(On-Budget Off-Budget Closed).freeze
  validates :name, uniqueness: true, presence: true
  validates :account_type, presence: true
  validates :account_type, inclusion: ACCOUNT_TYPES
  has_many :register_transactions, dependent: :nullify
  attr_accessor :date

end

app/models/category.rb

class Category < ApplicationRecord
  belongs_to :parent, class_name: "Category", optional: true
  has_many :subcategories, class_name: "Category", foreign_key: :parent_id
  scope :top_level, -> { where(parent_id: nil) }

  validates :name, uniqueness: true, presence: true

  has_many :budget_transactions, dependent: :nullify
  has_many :register_transactions, dependent: :nullify

end

加载页面时的服务器响应'register_transactions/new'

Started GET "/register_transactions/new" for 127.0.0.1 at 2022-05-28 17:41:08 -0700
Processing by RegisterTransactionsController#new as HTML
  Rendering layout layouts/application.html.erb
  Rendering register_transactions/new.html.erb within layouts/application
  Category Load (0.3ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" IS NULL
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.2ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 1]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.1ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 2]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.2ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 3]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.2ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 4]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.1ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 5]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (1.8ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 6]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.1ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 7]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.3ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 8]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Category Load (0.1ms)  SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" = ?  [["parent_id", 9]]
  ↳ app/views/register_transactions/_form.html.erb:7
  Vendor Load (0.3ms)  SELECT "vendors".* FROM "vendors" ORDER BY "vendors"."name" ASC
  ↳ app/views/register_transactions/_form.html.erb:16
  BankAccount Load (0.3ms)  SELECT "bank_accounts".* FROM "bank_accounts" ORDER BY "bank_accounts"."name" ASC
  ↳ app/views/register_transactions/_form.html.erb:26
  Rendered register_transactions/_form.html.erb (Duration: 35.7ms | Allocations: 15908)
  Rendered register_transactions/new.html.erb within layouts/application (Duration: 36.9ms | Allocations: 16216)
  Rendered layout layouts/application.html.erb (Duration: 40.3ms | Allocations: 18683)
Completed 200 OK in 43ms (Views: 38.4ms | ActiveRecord: 4.1ms | Allocations: 19653)

prompt 文本仅在没有为属性设置值时显示。您可以检查是否有任何默认值设置为 vendor_id.