验证使用控制器变量的 select 标记

Validate select tag that uses a controller variable

我的表单工作正常,但每当我添加表单验证时,我的 select 标签上总是出现 undefined method empty? for nil:NilClass。我发现我将控制器变量放入表单上的 select 函数导致了问题。

我有很多 select 标签的选项,所以尽管我知道如果我只是将数组粘贴到 select 函数中它会起作用,但我想找到一种方法来保留它和我现在一样干净

这是我的控制器

class MembersController < ApplicationController

  before_action :find_member, only: [:edit, :update, :delete, :destroy]
  before_action :form_vars, only: [:new, :edit, :executives]

  def find_member
    @members = Member.find(params[:id])
  end

  ...

  def new
    @members = Member.new
  end

  def create
    @members = Member.new(members_params) #the new Members entry is recreated through the strong params method
    @members.is_executive = true #Remove for members feature
    if @members.save #if the entry saves
      redirect_to({ action: 'executives' }, notice: @members.first_name + ' ' + @members.last_name + ' has been created!') #show all of the present entries
    else
      render({ action: 'new' }) #redisplay the create an entry page
    end
  end
end

这是我的表格

<div class="forms">
    <%= form_for(@members) do |member| %>
        <small>* = Required</small>
    <label>First Name*</label><br>
    <%= member.text_field :first_name %><br>
    <label>Last Name*</label><br>
    <%= member.text_field :last_name %><br>
    <label>LinkedIn Profile Address</label><br>
    <%= member.url_field :linkedinurl %><br>
        <label>Photo*</label><br>
        <%= member.file_field :avatar %><br>
    <%= member.label 'Major*' %><br>
    <%= member.select(:major, @major, selected: @members.major, prompt: 'Select Major') %><br>
    <label>E-Mail*</label><br>
    <%= member.text_field :email %><br>
    <label>Graduation Date</label><br>
    <%= member.select(:graduation_date, [1], selected: @members.graduation_date, prompt: 'Month') %>
        <%= member.select(:graduation_date, Date.today.year..Date.today.year+5, prompt: 'Year') %><br>
    <!-- <%= member.label(:is_executive, "Is this member an executive?") %>
    <%= member.check_box(:is_executive) %></br> -->
    <label>Executive Position*</label><br>
    <%= member.select(:executive_position, @executive, selected: @members.executive_position, prompt: 'Select Position') %><br>
        <%= member.submit %>
        <b><%= link_to "Cancel", '/members/executives'%></b>
    <% end %>
</div>

我的 select 标签选项是在应用程序控制器中声明的,因为我在其他模型上以多种形式使用相同的 select 标签

class ApplicationController < ActionController::Base

...
    def form_vars
      @majors = ['Accounting', 'Africana Studies', 'Anthropology', 'Architecture', 'Art', 'Art History', 'Athletic Training', 'Biology', 'Business Administration', 'Chemistry', 'Child and Family Development', 'Civil Engineering', 'Civil Engineering Technology', 'Computer Science', 'Computer Engineering', 'Construction Management', 'Criminal Justice', 'Dance', 'Earth and Environmental Sciences', 'Economics', 'Electrical Engineering', 'Electrical Engineering Technology', 'Elementary Education', 'English', 'Environmental Studies', 'Exercise Science', 'Finance', 'Fire Safety Engineering Technology', 'French', 'Geography', 'Geology', 'German', 'History', 'International Business', 'International Studies', 'Japanese', 'Latin American Studies', 'Management', 'Management Information Systems', 'Marketing', 'Mathematics', 'Mathematics for Business', 'Mechanical Engineering', 'Mechanical Engineering Technology', 'Meteorology', 'Middle Grades Education', 'Music', 'Neurodiagnostics and Sleep Science', 'Nursing', 'Ops & Supply Chain Management', 'Pre-Law', 'Philosophy', 'Physics', 'Political Science', 'Psychology', 'Public Health', 'Religious Studies', 'Respiratory Therapy', 'Social Work', 'Sociology', 'Spanish', 'Special Education', 'Systems Engineering', 'Theatre']
      @executive = ['President', 'Vice President', 'Treasurer', 'Secretary', 'Strategic Advisor', 'Director of Strategic Partnerships', 'Director of Programs', 'Director of Public Relations', 'Director of Publications', 'Director of Community Service', 'Director of Fundraising', 'Historian', 'Digital Administrator']
      @grad_month = ['May', 'December']
    end
end

执行此操作的最佳方法是什么,以便我可以将数组保留在我的表单文件之外但进行有效验证?我是 Rails 的新手,所以欢迎任何其他最佳实践技巧谢谢!

验证是 ActiveRecord 的一部分。因此,对于验证工作,您可以将所有需要的值添加到模型中,如下所示:

class Member < ActiveRecord::Base
  MAJORS = ['Accounting', 'Africana Studies', 'Anthropology', 'Architecture', 'Art', ...]
  EXECUTIVES = ['President', 'Vice President', 'Treasurer', 'Secretary', ...]
  GRAD_MONTH = ['May', 'December']

  validates :major, inclusion: { in: MAJORS, allow_blank: false }
  validates :executive_position, inclusion: { in: EXECUTIVES, allow_blank: false }
  validates :graduation_date, inclusion: { in: GRAD_MONTH, allow_blank: false }
end

然后在你的表单中:

<%= f.select(:executive_position, Member::EXECUTIVES, selected: @members.executive_position, prompt: 'Select Position') %>
# ...
<%= member.select(:graduation_date, Members::GRAD_MONTH, selected: @members.graduation_date, prompt: 'Month') %>
# ...
<%= member.select(:major, Member::MAJORS, selected: @members.major, prompt: 'Select Major') %><br>

这里是关于包含验证的documentation

使用该方法,您无需在 ApplicationController 中定义值。

希望对您有所帮助。