提问人:chug 提问时间:5/29/2022 更新时间:6/2/2022 访问量:75
3 个 Rails 表单 (collection_select) 元素中只有 2 个出现提示
Prompt appears for only 2 of 3 Rails form (collection_select) elements
问:
我是 Rails 初学者。 我有一个包含多个collection_select元素(类别、银行帐户、供应商)的表单。“类别”和“银行帐户”字段正确显示提供的提示,但“供应商”字段不显示其提示。相反,将显示第一个供应商,并且提示根本不出现在该下拉列表中。
我很困惑,因为“银行帐户”字段非常相似,但它可以正常工作。
app/views/register_transactions/new.html.erb
<div>
<h1>New Register Transaction</h1>
<%= render 'form', register_transaction: @register_transaction %>
</div>
应用程序/视图/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 %>
应用程序/控制器/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
应用/模型/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
应用/模型/类别.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)
答:
0赞
Vishal Jain
6/2/2022
#1
prompt
仅当未为属性设置值时,才会显示文本。您可以检查是否有任何默认值设置为 .vendor_id
评论
t.integer "vendor_id", default: 0