I'm a Rails beginner. I have a form with several collection_select elements (Category, Bank Account, Vendor). The fields for Category and Bank Account correctly display the supplied prompt, but the Vendor field does not display its prompt. Instead the first Vendor is shown, and the prompt does not appear in that drop down list at all.
I'm very confused because the Bank Account field is very similar but it works correctly.
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
The server response when I load the page '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)
CodePudding user response:
prompt
text is only shown if there is no value set for the attribute. You can check if there is any default value set to the vendor_id
.