0

我的表单工作正常,但undefined method empty? for nil:NilClass每当我添加表单验证时,我都会继续使用我的选择标签。我发现我将控制器变量放入表单上的选择函数的事实导致了问题。

我有很多用于选择标签的选项,所以即使我知道如果我只是将数组粘贴到选择函数中它会起作用,我想找到一种方法让它像我现在一样保持干净

这是我的控制器

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>

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

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 的新手,所以欢迎任何其他最佳实践技巧谢谢!

4

1 回答 1

0

验证是 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>

这是有关包含验证的文档。

使用该方法,您无需在ApplicationController.

希望这会有所帮助。

于 2016-06-05T06:29:44.907 回答