【问题标题】:Ruby on Rails: Drop down menuRuby on Rails:下拉菜单
【发布时间】:2009-12-24 01:06:11
【问题描述】:

我正在尝试创建一个下拉菜单,以允许用户更改表中的条目字段。用户有三个选项之一——热、中和冷。

我已经有text_fields 对其他字段执行基本相同的操作,当用户单击submit_tag 时,所有这些都会更新。

有没有一种简单的方法来实现一个下拉框并使用submit_tag 保存结果?

谢谢,

-克里斯

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    这是基本答案。二元数组的数组是关键部分。

    <% form_for @entry do |f| %>
      <%= f.text_field :name %>
      <%= f.select :temperature, [['Hot','hot'],['Medium','medium'],['Cold','cold']] %>
      <%= f.submit %>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      我会假设两件事:

      • 您就是&lt;%= form_for @model_instance 成语(在this guide 的第2.2 节中解释)。
      • 您希望将“热”、“中”和“冷”值存储为数据库中的字符串(而不是数字 1,2 和 3 或类似的东西)。

      假设您有两个字段,分别称为:name:temperature,由两个text_fields 控制:

      <% form_for @article do |f| %>
        <%= f.text_field :name %>
        <%= f.text_field :temperature %>
        <%= f.submit "Create" %> <% end %>
      <% end %>
      

      现在您想将 :temperature 控件更改为下拉列表,接受热、中和冷作为值。然后你可以这样做:

      <% form_for @article do |f| %>
        <%= f.text_field :name %>
        <%= f.collection_select :temperature, Article::TEMPERATURES, :to_s, :to_s, 
             :include_blank => true
        %>
        <%= f.submit "Create" %> <% end %>
      <% end %>
      

      您现在必须在 Article 模型中定义 Article::TEMPERATURES 常量。应该不难:

      class Article < Activerecord::Base
      
        TEMPERATURES = ['hot', 'medium', 'cold']
      

      您可能想知道为什么我在collection_select 上添加了:include_blank 部分。这将在您的下拉列表中添加一个“空”选项。创建新对象时您将需要该空选项,除非您想要温度的“默认”值。

      【讨论】:

        【解决方案3】:
        【解决方案4】:

        我正在做类似的事情。我通过在我的模型中简单地添加一个枚举或一个常量(类似于 kikito 之前所说的)然后在我的表单中调用 select 来实现这一点。

        这是它的工作原理。

        使用常量:

        class 类名

        bin/rails g 迁移 add_column_to_table 温度:字符串

        _form.html.erb

        <%= f.label :temperature %>
        <%= f.select :temperature, ClassName::TEMPERATURE %>
        

        使用枚举:

        class 类名

        bin/rails g 迁移 add_column_to_table 温度:整数

        _form.html.erb

        <%= f.label :temperature %>
        <%= f.select :temperature, ClassName.temperatures.keys %>
        

        希望对你有所帮助!

        【讨论】:

          【解决方案5】:

          您可能需要考虑formtastic gem,它的代码要少得多。

          <% semantic_form_for @stuff do |f| %>  
             <% f.inputs do %>  
             <%= f.input :name %>  
             <%= f.input :temperature, :as => :select, 
                         :label => "Degree", :include_blank => false,
                         :collection => [["Hot", 1], ["Medium", 2], ["Cold", 3]] %>  
             <% end %>  
             <%= f.buttons %>    
          <% end %>  
          

          【讨论】:

            【解决方案6】:

            根据以上所有答案,记得做最后一步,重要的一步:

            重启你的服务器

            作为一个新手,我想知道为什么即使我正确执行了所有步骤,我的阵列也无法正常工作。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-08-04
              • 2021-07-27
              • 2023-03-10
              • 1970-01-01
              • 2013-06-28
              • 2015-11-25
              • 1970-01-01
              相关资源
              最近更新 更多