【问题标题】:Rails 3.x dropdowns for modelsRails 3.x 模型下拉菜单
【发布时间】:2011-10-31 18:57:40
【问题描述】:

所以我正在运行 rails 3.1.0.rc6,我很难理解模型的下拉(选择元素)。

我有以下几点:

<%= form_for @user do |f| %>
      <div class="input">
        <%= f.label :email %>
        <%= f.text_field :email %>
      </div>

      <div class="input">
        <%= f.label :password %>
        <%= f.password_field :password %>
      </div>

      <div class="input">
        <%= f.label :password_confirmation, "Repeat Password" %>
        <%= f.password_field :password_confirmation %>
      </div>

      <div class="input">
        <%= f.label :first_name %>
        <%= f.password_field :first_name %>
      </div>

      <div class="input">
        <%= f.label :last_name %>
        <%= f.password_field :last_name %>
      </div>

      <div class="input">
        <%= f.label :birthday %>
        <%= date_select "user", "birthday" %>
      </div>

      <div class="input">
        <%= f.label :gender %>
        <%= select_tag :gender, options_for_select([['Male', 'male'], ['Female', 'female']]) %>
      </div>

      <div class="submit">
        <%= f.submit nil, :class => ['button', 'button_blue'] %>
      </div>
    <% end %>

现在我不明白如何有一个易于使用的生日选择和性别选择。它生成的 HTML 与模型保存一起使用并不完全友好。

任何帮助都会很棒!谢谢。

目标:只需能够使用发布的数据进行简单的 User.create。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    强烈建议您使用 simple_form(基于 formtastic)。每天有成千上万的开发人员使用它,有 45 个分支等,是我合作过的大多数组织的标准。你永远不会回头看它是如此简单(但灵活)。由 Jose Valim 撰写,他撰写了有关 Rails 的书籍,并且是社区的领导者之一。

    https://github.com/plataformatec/simple_form
    

    小摘录(解决您关于模型下拉(选择元素)的具体问题:-

    ...

    Now we have the user form:
    <%= simple_form_for @user do |f| %>
      <%= f.input :name %>
      <%= f.association :company %>
      <%= f.association :roles %>
      <%= f.button :submit %>
    <% end %>
    
    Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well:
    f.association :company, :as => :radio
    f.association :roles,   :as => :check_boxes
    

    ...

    您仍然可以将 date_calendar gem 用作一个不错的日历弹出日期选择器,这个Does anyone know any gems/plugins/tutorials related to exporting events to iCal, Google Calendar, Outlook from a Rails application? 也可能会有所帮助

    【讨论】:

    • 谢谢,我去看看!不过,就目前而言,我可能只使用 f.select 作为我的解决方案。
    【解决方案2】:

    对于你的日期选择,你可以使用jquery datepicker,它使用起来非常友好。

    首先,如果你这样写你的表单域

    <div class="field">
        <%= f.label :birthday %><br />
        <%= f.text_field :birthday %>
    </div>
    

    它会自动获得 id='user_birthday'(注意:注意将其设置为文本字段)。 然后使用 jquery 在 javascript 中添加一个简单的配置:

    $(document).ready(function() {
    
        $.datepicker.setDefaults({
            dateFormat: 'dd/mm/yy',
            firstDay: 1,
            showOn: 'focus'
        }); 
    
        $('#user_birthday').datepicker();
    
    });
    

    【讨论】:

    • 这很好,但人们习惯于使用下拉菜单来表示生日之类的东西。年代太久远了,人们不想使用这样的选择器。
    【解决方案3】:

    您可以直接从您的“f”变量中使用选择表单构建器帮助程序

    f.select( :gender, options_for_select([["Male", "male"],["Female","female"]]))
    

    这将确保选择的名称和 ID 以这样一种方式生成,您的创建将自动获取它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多