Ruby-on-rails 如何在 rails 表单中有一个下拉 <select> 字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14113057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have a drop down <select> field in a rails form?
提问by iCyborg
I am creating a scaffold -
我正在创建一个脚手架 -
rails g scaffold Contact email:string email_provider:string
but I want the email provider to be a drop down (with gmail/yahoo/msn as options) and not a text field. How can I do this ?
但我希望电子邮件提供商是一个下拉列表(使用 gmail/yahoo/msn 作为选项)而不是文本字段。我怎样才能做到这一点 ?
回答by R Milushev
You can take a look at the Rails documentation. Anyways , in your form :
您可以查看Rails 文档。无论如何,以您的形式:
<%= f.collection_select :provider_id, Provider.order(:name),:id,:name, include_blank: true %>
As you can guess , you should predefine email-providers in another model -Provider, to have where to select them from .
您可以猜到,您应该在另一个模型中预定义电子邮件提供商 - Provider,以便从中选择它们。
回答by Fdwillis
Or for custom options
或自定义选项
<%= f.select :desired_attribute, ['option1', 'option2']%>
回答by Michael Durrant
You create the collection in the Contactcontroller -
您在Contact控制器中创建集合-
app/controllers/contacts_controller.erb
Adding
添加
@providers = Provider.all.by_name
to the new, create and edit methods, using a scope for the by_namein the Providermodel - app/models/provider.rb- for the ordering by name
新,创建和编辑方法,使用范围为by_name在Provider模型- app/models/provider.rb-按名称排序
scope by_name order(:name)
Then in the view - app/views/contacts/_form.html.erb- you use
然后在视图中app/views/contacts/_form.html.erb——你使用
<%= f.collection_select :provider_id, @providers, :id, :name, include_blank: true %>
For rails forms, I also strongly recommend you look at a form builder like simple_form - https://github.com/plataformatec/simple_form- which will do all the heavy lifting.
对于 rails 表单,我还强烈建议您查看像 simple_form 这样的表单构建器 - https://github.com/plataformatec/simple_form- 它将完成所有繁重的工作。
回答by phobos
This is a long way round, but if you have not yet implemented then you can originally create your models this way. The method below describes altering an existing database.
这是一个很长的路要走,但是如果您还没有实现,那么您最初可以通过这种方式创建模型。下面的方法描述了更改现有数据库。
1) Create a new model for the email providers:$ rails g model provider name
1)为电子邮件提供商创建一个新模型:$ rails g model provider name
2) This will create your model with a name string and timestamps. It also creates the migration which we need to add to the schema with:$ rake db:migrate
2) 这将使用名称字符串和时间戳创建您的模型。它还创建了我们需要添加到架构中的迁移:$ rake db:migrate
3) Add a migration to add the providers ID into the Contact:$ rails g migration AddProviderRefToContacts provider:references
3) 添加迁移以将提供者 ID 添加到联系人中:$ rails g migration AddProviderRefToContacts provider:references
4) Go over the migration file to check it look OK, and migrate that too:$ rake db:migrate
4)检查迁移文件以检查它是否正常,然后也迁移它:$ rake db:migrate
5) Okay, now we have a provider_id, we no longer need the original email_provider string:$ rails g migration RemoveEmailProviderFromContacts
5) 好的,现在我们有了 provider_id,我们不再需要原来的 email_provider 字符串:$ rails g migration RemoveEmailProviderFromContacts
6) Inside the migration file, add the change which will look something like:
6) 在迁移文件中,添加如下所示的更改:
class RemoveEmailProviderFromContacts < ActiveRecord::Migration
def change
remove_column :contacts, :email_provider
end
end
7) Once that is done, migrate the change:$ rake db:migrate
7) 完成后,迁移更改:$ rake db:migrate
8) Let's take this moment to update our models:
Contact: belongs_to :provider
Provider: has_many :contacts
8) 让我们花点时间更新我们的模型:
联系方式:belongs_to :provider
提供者:has_many :contacts
9) Then, we set up the drop down logic in the _form.html.erb partial in the views:
9) 然后,我们在视图的_form.html.erb 部分中设置下拉逻辑:
<div class="field">
<%= f.label :provider %><br>
<%= f.collection_select :provider_id, Provider.all, :id, :name %>
</div>
10) Finally, we need to add the provders themselves. One way top do that would be to use the seed file:
10)最后,我们需要自己添加提供者。这样做的一种方法是使用种子文件:
Provider.destroy_all
gmail = Provider.create!(name: "gmail")
yahoo = Provider.create!(name: "yahoo")
msn = Provider.create!(name: "msn")
$ rake db:seed
$ rake db:seed
回答by Raghvendra Parashar
Please have a look here
请看这里
Either you can use rails tag Or use plain HTML tags
您可以使用 rails 标记或使用纯 HTML 标记
Rails tag
Rails 标签
<%= select("Contact", "email_provider", Contact::PROVIDERS, {:include_blank => true}) %>
*above line of code would become HTML code(HTML Tag), find it below *
*上面的代码行会变成 HTML 代码(HTML 标签),在下面找到它 *
HTML tag
HTML 标签
<select name="Contact[email_provider]">
<option></option>
<option>yahoo</option>
<option>gmail</option>
<option>msn</option>
</select>
回答by gsumk
<%= f.select :email_provider, ["gmail","yahoo","msn"]%>
<%= f.select :email_provider, ["gmail","yahoo","msn"]%>
回答by konyak
In your model,
在你的模型中,
class Contact
self.email_providers = %w[Gmail Yahoo MSN]
validates :email_provider, :inclusion => email_providers
end
In your form,
在你的表格中,
<%= f.select :email_provider,
options_for_select(Contact.email_providers, @contact.email_provider) %>
the second arg of the options_for_select will have any current email_provider selected.
options_for_select 的第二个参数将选择任何当前的 email_provider。
回答by sivamani
Rails drop down using has_many association for article and category:
Rails 下拉使用 has_many 关联文章和类别:
has_many :articles
belongs_to :category
<%= form.select :category_id,Category.all.pluck(:name,:id),{prompt:'select'},{class: "form-control"}%>

