如何在rails表格中下拉<select>字段?</select>

时间:2013-01-01 18:27:49

标签: ruby-on-rails ruby-on-rails-3 drop-down-menu input selectlist

我正在创建一个脚手架 -

rails g scaffold Contact email:string email_provider:string 

但我希望电子邮件提供商是一个下拉列表(使用gmail / yahoo / msn作为选项),而不是文本字段。我怎样才能做到这一点 ?

8 个答案:

答案 0 :(得分:80)

您可以查看the Rails documentation。无论如何,以你的形式:

  <%= f.collection_select :provider_id, Provider.order(:name),:id,:name, include_blank: true %>

您可以猜测,您应该预先定义另一个模型中的电子邮件提供商 - Provider,以便从中选择它们。

答案 1 :(得分:42)

或自定义选项

  libraries ptf77blas,ptcblas,atlas not found in C:\Python34\lib

  libraries lapack_atlas not found in C:\Python34\lib

  libraries ptf77blas,ptcblas,atlas not found in C:\

  libraries lapack_atlas not found in C:\

  libraries ptf77blas,ptcblas,atlas not found in C:\Python34\libs

  libraries lapack_atlas not found in C:\Python34\libs



  NOT AVAILABLE



atlas_info:

  libraries f77blas,cblas,atlas not found in C:\Python34\lib

  libraries lapack_atlas not found in C:\Python34\lib

  libraries f77blas,cblas,atlas not found in C:\

  libraries lapack_atlas not found in C:\

  libraries f77blas,cblas,atlas not found in C:\Python34\libs

  libraries lapack_atlas not found in C:\Python34\libs



  NOT AVAILABLE



lapack_info:

  libraries lapack not found in ['C:\\Python34\\lib', 'C:\\', 'C:\\Python34\\lib
s']

  NOT AVAILABLE



lapack_src_info:

  NOT AVAILABLE



  NOT AVAILABLE



running install

running build

running config_cc

unifing config_cc, config, build_clib, build_ext, build commands --compiler opti
ons

running config_fc

unifing config_fc, config, build_clib, build_ext, build commands --fcompiler opt
ions

running build_src

build_src

building py_modules sources

creating build

creating build\src.win-amd64-3.4

creating build\src.win-amd64-3.4\numpy

creating build\src.win-amd64-3.4\numpy\distutils

building library "npymath" sources

No module named 'numpy.distutils.msvccompiler' in numpy.distutils; trying from d
istutils

Running from numpy source directory.

C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutils\system
_info.py:1603: UserWarning:

    Atlas (http://math-atlas.sourceforge.net/) libraries not found.

    Directories to search for the libraries can be specified in the

    numpy/distutils/site.cfg file (section [atlas]) or by setting

    the ATLAS environment variable.

  warnings.warn(AtlasNotFoundError.__doc__)

C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutils\system
_info.py:1612: UserWarning:

    Blas (http://www.netlib.org/blas/) libraries not found.

    Directories to search for the libraries can be specified in the

    numpy/distutils/site.cfg file (section [blas]) or by setting

    the BLAS environment variable.

  warnings.warn(BlasNotFoundError.__doc__)

C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutils\system
_info.py:1615: UserWarning:

    Blas (http://www.netlib.org/blas/) sources not found.

    Directories to search for the sources can be specified in the

    numpy/distutils/site.cfg file (section [blas_src]) or by setting

    the BLAS_SRC environment variable.

  warnings.warn(BlasSrcNotFoundError.__doc__)

C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutils\system
_info.py:1505: UserWarning:

    Atlas (http://math-atlas.sourceforge.net/) libraries not found.

    Directories to search for the libraries can be specified in the

    numpy/distutils/site.cfg file (section [atlas]) or by setting

    the ATLAS environment variable.

  warnings.warn(AtlasNotFoundError.__doc__)

C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutils\system
_info.py:1516: UserWarning:

    Lapack (http://www.netlib.org/lapack/) libraries not found.

    Directories to search for the libraries can be specified in the

    numpy/distutils/site.cfg file (section [lapack]) or by setting

    the LAPACK environment variable.

  warnings.warn(LapackNotFoundError.__doc__)

C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutils\system
_info.py:1519: UserWarning:

    Lapack (http://www.netlib.org/lapack/) sources not found.

    Directories to search for the sources can be specified in the

    numpy/distutils/site.cfg file (section [lapack_src]) or by setting

    the LAPACK_SRC environment variable.

  warnings.warn(LapackSrcNotFoundError.__doc__)

C:\Python34\lib\distutils\dist.py:260: UserWarning: Unknown distribution option:
 'define_macros'

  warnings.warn(msg)

Traceback (most recent call last):

  File "", line 1, in 

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\setup.py", lin
e 251, in 

    setup_package()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\setup.py", lin
e 243, in setup_package

    setup(**metadata)

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\core.py", line 169, in setup

    return old_setup(**new_attr)

  File "C:\Python34\lib\distutils\core.py", line 148, in setup

    dist.run_commands()

  File "C:\Python34\lib\distutils\dist.py", line 955, in run_commands

    self.run_command(cmd)

  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command

    cmd_obj.run()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\install.py", line 62, in run

    r = self.setuptools_run()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\install.py", line 36, in setuptools_run

    return distutils_install.run(self)

  File "C:\Python34\lib\distutils\command\install.py", line 539, in run

    self.run_command('build')

  File "C:\Python34\lib\distutils\cmd.py", line 313, in run_command

    self.distribution.run_command(command)

  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command

    cmd_obj.run()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\build.py", line 39, in run

    old_build.run(self)

  File "C:\Python34\lib\distutils\command\build.py", line 126, in run

    self.run_command(cmd_name)

  File "C:\Python34\lib\distutils\cmd.py", line 313, in run_command

    self.distribution.run_command(command)

  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command

    cmd_obj.run()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\build_src.py", line 153, in run

    self.build_sources()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\build_src.py", line 164, in build_sources

    self.build_library_sources(*libname_info)

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\build_src.py", line 299, in build_library_sources

    sources = self.generate_sources(sources, (lib_name, build_info))

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\build_src.py", line 386, in generate_sources

    source = func(extension, build_dir)

  File "numpy\core\setup.py", line 684, in get_mathlib_info

    st = config_cmd.try_link('int main(void) { return 0;}')

  File "C:\Python34\lib\distutils\command\config.py", line 243, in try_link

    self._check_compiler()

  File "C:\Users\JOOPED~1\AppData\Local\Temp\pip_build_Root\numpy\numpy\distutil
s\command\config.py", line 57, in _check_compiler

    self.compiler.initialize()

  File "C:\Python34\lib\distutils\msvc9compiler.py", line 371, in initialize

    vc_env = query_vcvarsall(VERSION, plat_spec)

  File "C:\Python34\lib\distutils\msvc9compiler.py", line 287, in query_vcvarsal
l

    raise ValueError(str(list(result.keys())))

ValueError: ['path']

答案 2 :(得分:14)

您可以在Contact控制器中创建集合 -

app/controllers/contacts_controller.erb 

添加

@providers = Provider.all.by_name

到新的,创建和编辑方法,使用by_name模型中Provider的范围 - app/models/provider.rb - 按名称排序

scope by_name  order(:name)

然后在视图中 - app/views/contacts/_form.html.erb - 您使用

<%= f.collection_select :provider_id, @providers, :id, :name, include_blank: true %>

对于rails表单,我还强烈建议您查看一个表单生成器,例如simple_form - https://github.com/plataformatec/simple_form - 这将完成所有繁重的工作。

答案 3 :(得分:8)

这是一个漫长的过程,但如果你还没有实现,那么你最初可以用这种方式创建你的模型。以下方法描述了更改现有数据库。

1)为电子邮件提供商创建新模型:
$ rails g model provider name

2)这将使用名称字符串和时间戳创建模型。它还创建了我们需要添加到模式的迁移:
$ rake db:migrate

3)添加迁移以将提供者ID添加到联系人中:
$ rails g migration AddProviderRefToContacts provider:references

4)浏览迁移文件以检查它是否正常,并将其迁移:
$ rake db:migrate

5)好的,现在我们有了一个provider_id,我们不再需要原始的email_provider字符串了:
$ rails g migration RemoveEmailProviderFromContacts

6)在迁移文件中,添加如下所示的更改:

class RemoveEmailProviderFromContacts < ActiveRecord::Migration
  def change
    remove_column :contacts, :email_provider
  end
end

7)完成后,迁移更改:
$ rake db:migrate

8)让我们花点时间来更新我们的模型:
联系方式:belongs_to :provider
提供者:has_many :contacts

9)然后,我们在视图中的_form.html.erb partial中设置下拉逻辑:

  <div class="field">
    <%= f.label :provider %><br>
    <%= f.collection_select :provider_id, Provider.all, :id, :name %>
  </div>

10)最后,我们需要自己添加证明者。最好的方法是使用种子文件:

Provider.destroy_all

gmail = Provider.create!(name: "gmail")
yahoo = Provider.create!(name: "yahoo")
msn = Provider.create!(name: "msn")

$ rake db:seed

答案 4 :(得分:3)

请查看here

您可以使用rails标记或使用纯HTML标记

Rails标记

<%= select("Contact", "email_provider", Contact::PROVIDERS, {:include_blank => true}) %>

* 以上代码行将成为HTML代码(HTML代码),在下面找到*

HTML标记

<select name="Contact[email_provider]">
  <option></option>
  <option>yahoo</option>
  <option>gmail</option>
  <option>msn</option>
</select>

答案 5 :(得分:3)

在你的模特中,

class Contact
  self.email_providers = %w[Gmail Yahoo MSN]
  validates :email_provider, :inclusion => email_providers
end

在您的表单中,

<%= f.select :email_provider, 
    options_for_select(Contact.email_providers, @contact.email_provider) %>

options_for_select的第二个arg将选择任何当前的email_provider。

答案 6 :(得分:3)

使用has_many关联文章和类别的下拉列表:

has_many :articles

belongs_to :category

<%= form.select :category_id,Category.all.pluck(:name,:id),{prompt:'select'},{class: "form-control"}%>

答案 7 :(得分:0)

<%= f.select :email_provider, ["gmail","yahoo","msn"]%>