undefined method empty? for nil:NilClass
フォームは正常に動作しますが、フォームの検証を追加するたびに 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>
他のモデルの複数のフォームで同じ選択タグを使用しているため、選択タグオプションはアプリケーションコントローラーで宣言されています
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 を初めて使用するので、他のベスト プラクティスのヒントを歓迎します ありがとう!