gpt4 book ai didi

ruby-on-rails - Rails 从数据库中选择

转载 作者:行者123 更新时间:2023-12-04 05:42:32 25 4
gpt4 key购买 nike

有人能解释一下吗? , 它来自 ruby​​ 指南

<%= collection_select(:person, :city_id, City.all, :id, :name) %>

我有一个附件模型,我想在创建对象时使用组合框选择它的版本,并且我还想要一个新的版本选项。

here what attachment has

def change
create_table :attachments do |t|
t.string :filename
t.attachment :file
t.string :version
t.text :description
t.timestamps null: false
end

UPDATE:

<%= f.collection_select( :version,  Attachment.where.not(version: nil), :version, :version) %>

它是这样工作的,但我不明白,

最佳答案

试试这个来避免 versionnil 值:

collection_select(:f, :attachment_id, Attachment.where.not(version: nil), :id, :version)

collection_select 工作原理说明:

collection_select(
:f, # field namespace
:attachment_id, # field name
# result of these two params will be: <select name="f[attachment_id]">...

# then you should specify some collection or array of rows.
# In your example it is:
Attachment.where.not(version: nil)

# Then, you should specify methods for generating options
:id, # this is name of method that will be called for every row, result will be set as key
:version # this is name of method that will be called for every row, result will be set as value
)

参见 thisthis获取更多信息。

关于ruby-on-rails - Rails 从数据库中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com