前提・実現したいこと
railsの学習をしております。
登録処理でValidationにかかった後のrenderで
select_tagがエラーになります。
※その他の項目は入力値がそのまま残っています
form_forでhtmlを組み立てていますが
プルダウンについてはモデルと関連性がないため
select_tagを使用しています。
やり方が間違っていないか、
解決方法をご教授いただければ幸甚です。
登録画面表示:
EmployeeController#new
登録処理:
EmployeeController#create
発生している問題・エラーメッセージ
undefined method `map' for nil:NilClass <%= select_tag :store1, options_from_collection_for_select(@storeAll, :id, :name), prompt: "選択してください", class: "form-control" %>
該当のソースコード
db/schema.rb # employees:社員管理テーブル # responsibles:社員管理テーブルと拠点テーブルの中間テーブル # stores:拠点テーブル ActiveRecord::Schema.define(version: 2018_12_02_020333) do create_table "employees", force: :cascade do |t| t.string "name" t.string "email" t.date "birthday" t.date "joinday" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "password_digest" t.string "sex" end create_table "responsibles", force: :cascade do |t| t.integer "order" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "employee_id" t.integer "store_id" t.index ["employee_id"], name: "index_responsibles_on_employee_id" t.index ["store_id"], name: "index_responsibles_on_store_id" end create_table "stores", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end
app/model/employee.rb class Employee < ApplicationRecord has_secure_password has_many :responsibles has_many :stores, through: :responsibles accepts_nested_attributes_for :responsibles validates :name, {presence: true, length: { maximum: 30 }} validates :email, {presence: true, uniqueness: true} validates :password, {presence: true, length: { minimum: 6 }} end app/model/responsible.rb class Responsible < ApplicationRecord belongs_to :employee belongs_to :store end app/model/store.rb class Store < ApplicationRecord has_many :responsibles has_many :employees, through: :responsibles end
app/config/routes.rb Rails.application.routes.draw do get "employee/new" => "employee#new" post "employee/create" => "employee#create" end
app/Controllers/employee_Controller.rb class EmployeeController < ApplicationController # 新規登録画面表示 def new @employee = Employee.new @storeAll = Store.all end # 新規登録 def create # 社員情報を登録 @employee = Employee.new @employee.name = params[:name] @employee.email = params[:email] @employee.password = params[:password] @employee.sex = params[:sex] @employee.birthday = params[:birthday] @employee.joinday = params[:joinday] # 担当拠点1を登録 if params[:store1].present? setResponsible(params[:store1], 1) end if @employee.save flash[:notice] = "新規作成しました" redirect_to("/employee/index") else render("employee/new") end end end
app/views/employee/new.html.erb <% @employee.errors.full_messages.each do |message| %> <div class="pt-xl-2"></div> <div class="alert alert-danger" role="alert"> ・<%= message %> </div> <% end %> <div class="mx-auto pt-xl-5" style="width:600px;"> <%= form_for(@employee, url: {controller: "employee", action:"create" }) do |f| %> <h1>Create</h1> <div class="form-label-group pt-xl-4"> <%= f.label :inputName, "名前" %> <%= f.text_field :name, id:"inputName",class: "form-control", placeholder: "名前", autocomplete:"new-name", required: true, autofocus:true %> </div> <div class="form-label-group pt-xl-3"> <%= f.label :inputEmail, "Email" %> <%= f.text_field :email, id:"inputEmail",type:"email", class: "form-control", placeholder: "Email", autocomplete:"new-email", required: true %> </div> <div class="form-label-group pt-xl-3"> <%= f.label :inputPassword, "Password" %> <%= f.text_field :password, id:"inputPassword",type:"password", class: "form-control", placeholder: "Password", autocomplete:"new-password", required: true %> </div> <div class="form-label-group pt-xl-3"> <%= f.label :inputSex, "性別" %> </div> <div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary active"> <%= f.radio_button :sex, :M, id:"inputSexM" %> 男 </label> <label class="btn btn-secondary"> <%= f.radio_button :sex, :F, id:"inputSexF" %> 女 </label> </div> <div class="form-label-group pt-xl-3"> <%= f.label :inputStore1, "担当拠点1" %> </div> <div> <%= select_tag :store1, options_from_collection_for_select(@storeAll, :id, :name), prompt: "選択してください", class: "form-control" %> </div> <div class="pt-xl-5 mb-lg-5"> <%= f.submit :create, class:"btn btn-lg btn-primary btn-block" %> </div> <% end %> </div>
試したこと
view
storeAll→Store.all
before:
<%= select_tag :store1, options_from_collection_for_select(@storeAll, :id, :name), prompt: "選択してください", class: "form-control" %>
after:
<%= select_tag :store1, options_from_collection_for_select(Store.all, :id, :name), prompt: "選択してください", class: "form-control" %>
補足情報(FW/ツールのバージョンなど)
開発端末:macOS Mojave
$ rails -v
Rails 5.2.1
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。