【问题标题】:How to create select for enum attribute in Rails 4 with simple Form如何使用简单的表单在 Rails 4 中为枚举属性创建选择
【发布时间】:2016-08-18 12:56:55
【问题描述】:

我很难弄清楚如何在 Rails 4.2 应用程序上使用 Simple_form 让我的选择字段适用于枚举属性。

这是我的模型:

class Employee < ActiveRecord::Base
  # returns the full name of the employee. This code is found in a concern called name.rb
  include Name

  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


  enum status: [:active, :vacation, :unemployed]

    enum os: [:mac, :windows]

    validates :first_name, 
            presence: true,
            length: {minimum: 2}

  validates :last_name, 
            presence: true,
            length: {minimum: 2}

    validates :email, email: true, presence: true, uniqueness: true
  validates :paypal_email, email: true, presence: true, uniqueness: true
    # validates :status, presence: true
  validates :skype_id, presence: true, uniqueness: true
  validates :mobile, presence: true, numericality: true, length: {minimum: 10}
  validates :address, :province_state, :country, :postal_code, :bachelor_degree, :os, presence: true #, , , ,, :os, presence: true

end

这是我的表格:

<h2>New Employee Registration Form</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :first_name, required: true, autofocus: true %>
    <%= f.input :last_name, required: true %>
    <%= f.input :email, required: true%>
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: true %>
    <%= f.input :paypal_email, required: true%>
    <%= f.input :mobile, required: true %>
    <%= f.input :skype_id, required: true %>
    <%= f.input :address, required: true %>
    <%= f.input :province_state, required: true %>
    <%= f.input :country, required: true %>
    <%= f.input :postal_code, required: true %>
    <%= f.input :bachelor_degree, required: true %>
    <%= f.input :os, as: :select, collection: Employee.os.keys, selected: 1, required: true %>
    <%= f.input :status, as: :select, collection: Employee.status.keys, selected: 1, required: true %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Register New Employee" %>
  </div>

这是我得到的错误:

失败/错误:

ActionView::Template::错误: #

的未定义方法“状态”

奇怪的是,类似字段的完全相同的代码只是 'os' 完美地工作。

感谢任何帮助找出此属性产生错误的原因。

【问题讨论】:

    标签: ruby-on-rails enums simple-form


    【解决方案1】:

    你可以这样做:

    f.input : os, :collection => Employee.statuses.keys.to_a

    你需要使用复数化版本的status。

    【讨论】:

    • 感谢 Supernini。请注意,我不需要使用 to_a 并且效果很好。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2016-08-02
    相关资源
    最近更新 更多